全局安装 npm 模块......然后节点无法找到它

Installing an npm module globally...and then node not being able to find it

我用 npm install -g zmq 安装了这个名为 zmq 的模块 假设我在一个新目录中并创建了一个简单的 node.js 文件:

//test.js

var zmq = require('zmq');
console.log(zmq);

...如果我 运行 这个,它将无法找到 zmq 包,即使我在全局安装了它。为了让脚本工作,我必须 运行 npm install zmq 没有 -g 标志,这会在同一目录中创建一个 node_modules 文件夹。我不希望那里有那个 node_modules 文件夹。

我看不出 -g 标志的用途,因为 none 我的节点程序似乎 运行 没有 node_modules 文件夹。

我在 Mac,但我在 Windows 和 Linux 上遇到了同样的问题。

我错过了什么?

来自 npm 的 website

  • Local install (default): puts stuff in ./node_modules of the current package root.
  • Global install (with -g): puts stuff in /usr/local or wherever node is installed.
  • Install it locally if you're going to require() it.
  • Install it globally if you're going to run it on the command line.

它是以这种方式构建的,因此每个项目都可以维护单独的依赖项,并且您可以轻松地在全球范围内确定哪个版本适用于哪个项目。

我不确定有没有办法做你想让它做的事,我不认为它会被推荐,即使有。

希望对您有所帮助!