I'm trying to write a Node script that makes use of the NPM API. 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.
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:
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
?
Finally solved by installing NPM locally to the directory. Thanks @SLaks!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.