node.js 如何在 Node 中导入全局模块?我收到“错误:找不到模块 <模块>”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7970793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:43:25  来源:igfitidea点击:

How do I import global modules in Node? I get "Error: Cannot find module <module>"?

node.jsnpm

提问by Hanpan

I am trying to setup Node on Mac OSX Lion. It all seems to work ok, but I can't seem to import anything modules from my global modules folder. I get the error,

我正在尝试在 Mac OSX Lion 上设置 Node。似乎一切正常,但我似乎无法从我的全局模块文件夹中导入任何模块。我得到错误,

Error: Cannot find module <module>

If I run this: node -e require.paths, the response I get is:

如果我运行这个: node -e require.paths,我得到的响应是:

[ '/usr/local/lib/node_modules',
  '/Users/Me/.node_modules',
  '/Users/Me/.node_libraries',
  '/usr/local/Cellar/node/0.4.12/lib/node' ]

Which is correct, my modules are indeed installed in /usr/local/lib/node_modules. When I try and run a script, however, I am getting this:

这是正确的,我的模块确实安装在 /usr/local/lib/node_modules 中。然而,当我尝试运行脚本时,我得到了这个:

Error: Cannot find module 'socket.io'
    at Function._resolveFilename (module.js:326:11)
    at Function._load (module.js:271:25)
    at require (module.js:355:19)
    at Object.<anonymous> (/Users/Me/node/server.js:2:10)
    at Module._compile (module.js:411:26)
    at Object..js (module.js:417:10)
    at Module.load (module.js:343:31)
    at Function._load (module.js:302:12)
    at Array.<anonymous> (module.js:430:10)
    at EventEmitter._tickCallback (node.js:126:26)

My .bash_profile looks like this:

我的 .bash_profile 看起来像这样:

export PATH=/usr/local/mysql/bin:$PATH
export NODE_PATH=/usr/local/lib/node_modules
export DYLD_LIBRARY_PATH="$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/"

Would really appreciate some help, I have no idea why I can't import any libraries.

非常感谢一些帮助,我不知道为什么我不能导入任何库。

采纳答案by Tadeusz Wójcik

If you're using npm >=1.0, you can use npm link <global-package>to create a local link to a package already installed globally. (Caveat: The OS must support symlinks.)

如果您使用的是 npm >=1.0,则可以使用npm link <global-package>创建指向已全局安装的包的本地链接。(警告:操作系统必须支持符号链接。

However, this doesn't come without its problems.

然而,这并非没有问题。

npm link is a development tool. It's awesomefor managing packages on your local development box. But deploying with npm link is basically asking for problems, since it makes it super easy to update things without realizing it.

npm link 是一个开发工具。这是真棒对当地的发展框管理包。但是使用 npm link 进行部署基本上是在寻找问题,因为它可以在不知不觉中更新内容变得非常容易。

As an alternative, you can install the packages locally as well as globally.

作为替代方案,您可以在本地和全局安装软件包。

For additional information, see

有关其他信息,请参阅

回答by Joel B

Node.js uses the environmental variable NODE_PATHto allow for specifying additional directories to include in the module search path. You can use npmitself to tell you where global modules are stored with the npm root -gcommand. So putting those two together, you can make sure global modules are included in your search path with the following command (on Linux-ish)

Node.js 使用环境变量NODE_PATH来允许指定要包含在模块搜索路径中的其他目录。您可以使用npm它自己来告诉您使用该npm root -g命令存储全局模块的位置。因此,将这两者放在一起,您可以使用以下命令(在 Linux-ish 上)确保搜索路径中包含全局模块

export NODE_PATH=$(npm root --quiet -g)

export NODE_PATH=$(npm root --quiet -g)

回答by Nick Sotiros

You can use npm link to create a symbolic link to your global package in your projects folder.

您可以使用 npm link 在您的项目文件夹中创建一个指向全局包的符号链接。

Example:

例子:

$ npm install -g express
$ cd [local path]/project
$ npm link express

All it does is create a local node_modules folder and then create a symlink express -> [global directory]/node_modules/express which can then be resolved by require('express')

它所做的只是创建一个本地 node_modules 文件夹,然后创建一个符号链接 express -> [global directory]/node_modules/express,然后可以通过 require('express')

回答by user5341372

Install any package globally as below:

全局安装任何软件包,如下所示:

$ npm install -g replace  // replace is one of the node module.

As this replace module is installed globally so if you see your node modules folder you would not see replace module there and so you can not use this package using require('replace').

因为这个替换模块是全局安装的,所以如果你看到你的节点模块文件夹,你就不会在那里看到替换模块,所以你不能使用 require('replace') 使用这个包。

because with require you can use only local modules which are present in your node module folder.

因为使用 require 您只能使用节点模块文件夹中存在的本地模块。

Now to use global module you should link it with node module path using below command.

现在要使用全局模块,您应该使用以下命令将其与节点模块路径链接。

$ npm link replace

Now go back and see your node module folder you could now be able to see replace module there and can use it with require('replace') in your application as it is linked with your local node module.

现在返回并查看您的节点模块文件夹,您现在可以在那里看到替换模块,并且可以在应用程序中将其与 require('replace') 一起使用,因为它与您的本地节点模块链接。

Pls let me know if any further clarification is needed.

请让我知道是否需要进一步澄清。

回答by ling

You can use require with the path to the global module directory as an argument.

您可以将 require 与全局模块目录的路径一起用作参数。

require('/path/to/global/node_modules/the_module');

On my mac, I use this:

在我的 mac 上,我使用这个:

require('/usr/local/lib/node_modules/the_module');

How to find where your global modules are? --> Where does npm install packages?

如何找到你的全局模块在哪里?--> npm 在哪里安装包?

回答by Ben Xu

Setting the environment variable NODE_PATH to point to your global node_modulesfolder.

将环境变量 NODE_PATH 设置为指向您的全局node_modules文件夹。

In Windows 7 or higher the path is something like %AppData%\npm\node_moduleswhile in UNIX could be something like /home/sg/.npm_global/lib/node_modules/but it depends on user configuration.

在 Windows 7 或更高版本中,路径类似于%AppData%\npm\node_modulesUNIX 中的路径,/home/sg/.npm_global/lib/node_modules/但它取决于用户配置。

The command npm config get prefixcould help finding out which is the correct path.

该命令npm config get prefix可以帮助找出哪个是正确的路径。

In UNIX systems you can accomplish it with the following command:

在 UNIX 系统中,您可以使用以下命令完成它:

export NODE_PATH=`npm config get prefix`/lib/node_modules/

回答by Darin London

I am using Docker. I am trying to create a docker image that has all of my node dependencies installed, but can use my local app directory at container run time (without polluting it with a node_modules directory or link). This causes problems in this scenario. My workaround is to require from the exact path where the module, e.g. require('/usr/local/lib/node_modules/socket.io')

我正在使用 Docker。我正在尝试创建一个安装了所有节点依赖项的 docker 映像,但可以在容器运行时使用我的本地应用程序目录(不会用 node_modules 目录或链接污染它)。这会导致在这种情况下出现问题。我的解决方法是从模块所在的确切路径中要求,例如 require('/usr/local/lib/node_modules/socket.io')

回答by Jamund Ferguson

require.pathsis deprecated.

require.paths已弃用。

Go to your project folder and type

转到您的项目文件夹并键入

npm install socket.io

that should install it in the local ./node_modules folder where node will look for it.

应该将它安装在本地 ./node_modules 文件夹中,节点将在该文件夹中查找它。

I keep my things like this:

我的东西是这样的:

cd ~/Sites/
mkdir sweetnodeproject
cd sweetnodeproject
npm install socket.io

Create an app.js file

创建一个 app.js 文件

// app.js
var socket = require('socket.io')

now run my app

现在运行我的应用程序

node app.js

Make sure you're using npm >= 1.0and node >= 4.0.

确保您正在使用npm >= 1.0node >= 4.0