[英]Accessing NPM module from Node using admin privileges (sudo)
I'm trying to write a Node script that makes use of the NPM API. 我正在尝试编写一个使用NPM API的Node脚本。 The script works, but requires admin access for certain modules to be installed.
该脚本有效,但是需要管理员权限才能安装某些模块。 I tried running the script with
sudo
, but it fails on the line requiring NPM. 我尝试使用
sudo
运行脚本,但是在需要NPM的行上失败。
I distilled it to the following simple script: 我将其提炼为以下简单脚本:
var npm = require('npm'),
assert = require('assert');
assert.notEqual(npm, null, 'NPM is null');
Running this script regularly with node npm.js
works, running it with sudo node npm.js
throws the following: 定期在
node npm.js
运行此脚本是node npm.js
,而在sudo node npm.js
运行它会引发以下情况:
Error: Cannot find module 'npm'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/Projects/MyNPM/npm.js:1:73)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
What am I missing, and what happens to the path structure when I use sudo
? 我缺少什么,使用
sudo
时路径结构会如何?
Finally solved by installing NPM locally to the directory. 最终通过将NPM本地安装到目录来解决。 Thanks @SLaks!
谢谢@SLaks!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.