npm安装后,Node无法找到依赖项。

4

我是Node.js的新手,在我的DigitalOcean VPS上遇到了这个问题,我的Web应用程序(Ghost)在安装特定模块后仍然提示找不到该模块:

root@3fen:/var/www/ghost# sudo npm install -g ghost-gql
ghost-gql@0.0.2 /usr/local/lib/node_modules/ghost-gql
└── lodash@3.10.1

root@3fen:/var/www/ghost# node index.js
ERROR: Ghost is unable to start due to missing dependencies:
  Cannot find module 'ghost-gql'
  Cannot find module 'jsonpath'

Please run `npm install --production` and try starting Ghost again.
Help and documentation can be found at http://support.ghost.org.

我确认在 package.json 的依赖项中都存在这两个条目,并且我也尝试了 npm install --production 命令,它静默执行并且依然出现相同的错误。
如果我忽略了任何步骤,请指正。谢谢!

你能否尝试运行Node REPL并查看require('ghost-gql')和require('jsonpath')是否正常工作? - prasun
@prasun 刚试了一下,但好像也不起作用:`root@3fen:/var/www/ghost# export PATH="$(npm bin):$PATH" root@3fen:/var/www/ghost# node
require("jsonpath") Error: 找不到模块'jsonpath'`
- Allan Jiang
@prasun /usr/local - Allan Jiang
@prasun 你是指 export NODE_PATH="/usr/local" 吗?即使这样似乎还是不起作用... :( - Allan Jiang
@prasun,它起作用了。谢谢你!! - Allan Jiang
显示剩余6条评论
1个回答

5

看起来全局npm路径未设置在环境变量NODE_PATH中。

这个路径应该是npm config get prefix的路径,再加上node_modules目录。

在您的情况下,

export NODE_PATH=/usr/local/node_modules

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接