节点包安装失败,因为节点在我的根目录查找相关性

当使用npm来安装软件包时,我最近遇到了一个问题,那就是Node无法find模块/脚本。 出于某种原因,它正在我的C:/目录,而不是C:/my-project/node_modules 。 这个图书馆特别麻烦,但我也看到过这种情况。

我正在运行Win7 x32 v0.10.35的Node

这是我尝试安装node-sass时看到的一个示例:

 C:\node\test>npm install node-sass > node-sass@2.0.0-beta install C:\node\test\node_modules\node-sass > node scripts/install.js module.js:340 throw err; ^ Error: Cannot find module 'C:\scripts\install.js' at Function.Module._resolveFilename (module.js:338:15) at Function.Module._load (module.js:280:25) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) at node.js:929:3 npm ERR! node-sass@2.0.0-beta install: `node scripts/install.js` npm ERR! Exit status 8 npm ERR! npm ERR! Failed at the node-sass@2.0.0-beta install script. npm ERR! This is most likely a problem with the node-sass package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! node scripts/install.js npm ERR! You can get their info via: npm ERR! npm owner ls node-sass npm ERR! There is likely additional logging output above. npm ERR! System Windows_NT 6.1.7601 npm ERR! command "C:\\Program Files\\nodejs\\\\node.exe" "C:\\Program Files\\nod ejs\\node_modules\\npm\\bin\\npm-cli.js" "install" "node-sass" npm ERR! cwd C:\node\test npm ERR! node -v v0.10.35 npm ERR! npm -v 1.4.28 npm ERR! code ELIFECYCLE npm ERR! not ok code 0 

一位同事把他的install.js文件发给我,我把它放在我的根目录下,看看会发生什么。 不幸的是,一旦npm尝试安装根模块需要的模块,安装仍会失败。

我尝试在我的Windows系统上安装node-sass时遇到了类似的问题。 我将其报告为节点问题(请参阅Windows 8.1 Pro 64位:由于node-sass@2.1.1安装脚本失败而导致的gulp-sass安装失败 )。

我添加了一些额外的日志logging到我的本地节点安装,以查看实际正在发生什么,并发现以下内容:

nodejs \ node_modules \ npm \ lib \ utils \ spawn.js用于启动脚本\ install.js通过添加一些额外的日志logging,我发现spawn.js正在使用以下命令行来启动install.js: C:\WINDOWS\system32\cmd.exe /c node scripts/install.js

在单独的命令提示符窗口中,我将其更改为我的c:\ test目录,然后运行C:\WINDOWS\system32\cmd.exe /c node以查看正在使用哪个目录节点。

在node>提示符下,我运行了process.cwd() 。 这显示: c:\\而不是c:\\test

当我看到c:\\上面时,我记得我在Windowsregistry中添加了一个Autorun设置到HKEY_CURRENT_USER\Software\Microsoft\Command Processor ,以便每次cmd.exe启动时自动运行一个启动脚本。 当我看到我的启动脚本,我注意到该脚本中的最后一个命令是: cd c:\ 。 这会导致每个cmd.exe实例都使用c:\​​作为其起始目录。

从我的启动脚本中删除cd c:\后,npm install node-sass运行正常。

我希望这对于遇到上述问题的人有帮助。

我不知道你的'前缀'configurationvariables是否设置。 你可以通过运行find

 > npm c get prefix 

它应该被设置为全球包裹的合理的东西; 如果它被设置为“C:\”那么这可能会导致你的问题。

您可以通过运行找出npm正在寻找configuration的位置

 > npm c get globalconfig > npm c get localconfig 

你可以列出所有的configurationvariables

 > npm c ls -l