【问题标题】:Node package installation failing because Node's looking in my root directory for dependacies节点包安装失败,因为节点在我的根目录中查找依赖项
【发布时间】:2015-06-26 23:13:28
【问题描述】:

在使用npm 安装软件包时,我最近遇到了一个问题,即弹出Node 找不到模块/脚本的错误。出于某种原因,它在我的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 尝试安装根模块所需的模块,安装仍然失败。

【问题讨论】:

  • 您有.npmrc 文件吗?如果有,请提供:)
  • @AlexFord 我以前没有听说过.npmrc 文件,只是开始查看它们。谢谢你的提示!哪一个最有价值?
  • 如果你没有,那么它是不相关的。它只是 NPM 的配置文件。不过,我对 Windows 上的节点不再那么熟悉了:/

标签: node.js filesystems npm node-modules


【解决方案1】:

我在尝试将 node-sass 安装到我的 Windows 系统时遇到了类似的问题。我将此报告为 node-sass 问题(请参阅 Windows 8.1 Pro 64-bit: gulp-sass install failing due to node-sass@2.1.1 install script failure)。

我在本地安装的节点中添加了一些额外的日志记录,以查看实际发生的情况并发现以下内容:

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

在一个单独的命令提示符窗口中,我切换到我的 c:\test 目录并运行 C:\WINDOWS\system32\cmd.exe /c node 以查看哪个目录节点正在使用。

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

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

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

我希望这对遇到上述问题中描述的问题的其他人有所帮助。

【讨论】:

  • 要去试试这个!感谢您的信息。
【解决方案2】:

我想知道您的“前缀”配置变量是否已设置。你可以通过运行找出来

> npm c get prefix

应该为全局包设置合理的值;如果它设置为“C:\”,那么这可能会导致您的问题。

你可以通过运行找到npm在哪里寻找配置

> npm c get globalconfig
> npm c get localconfig

并且可以列出所有的配置变量

> npm c ls -l

【讨论】:

  • 嗯,我刚试过这个。看起来我的前缀是正确的,因为它返回 C:\Users\jamesm\AppData\Roaming\npm
  • 尝试查看npm c ls -l 的输出,然后查看是否在某处不合理地设置为C:`. Another possibility - although it's unlikely if you didn't create it yourself, there may be a per-project .npmrc`,从而搞砸了。
猜你喜欢
  • 2014-11-27
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 1970-01-01
  • 2013-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多