node . js需要一个全局模块/包。

时间:2021-08-15 16:56:18

I'm trying to install globally and then use forever and forever-monitor like this:

我尝试在全球安装,然后使用永久和永久监控,像这样:

npm install -g forever forever-monitor

npm安装-g永远的监视。

I see the usual output and also the operations that copy the files to the global path, but then if I try to require("forever"); I get an error saying that the module wasn't found.

我看到了通常的输出和将文件复制到全局路径的操作,但如果我尝试要求(“永远”);我得到一个错误,说这个模块没有找到。

I'm using latest version of both node and npm and I already know about the change that npm made in global vs local install, but I really don't want to install localy on every project and I'm working on a platform that doesn't support link so npm link after a global install isn't possible for me.

我使用最新版本的节点和npm和我已经知道npm的变化在全球和本地安装,但是我真的不想在每个项目上安装场所,我工作在一个平台,不支持链接所以npm链接全球安装后对我是不可能的。

My question is: why I can't require a globally installed package? Is that a feature or a bug? Or am I doing something wrong?

我的问题是:为什么我不需要一个全球安装的软件包?这是一个特性还是一个错误?还是我做错了什么?

PS: Just to make it crystal clear: I don't want to install locally.

PS:我只是想把它弄清楚:我不想在本地安装。

7 个解决方案

#1


148  

In Node.js, require doesn't look in the folder where global modules are installed.

在节点。js,要求不查看安装全局模块的文件夹。

You can fix this by setting the NODE_PATH environment variable. In Linux this will be:

您可以通过设置NODE_PATH环境变量来解决这个问题。在Linux中,这将是:

export NODE_PATH=/usr/lib/node_modules

Note: This depend on where your global modules are actually installed.

注意:这取决于您的全局模块实际安装在哪里。

See: Loading from the global folders.

参见:从全局文件夹加载。

#2


74  

After you install package globally you have to link the local project with global package

在全球安装包之后,必须将本地项目与全局包链接起来。

npm install express -g
cd ~/mynodeproject/
npm link express  

See here

在这里看到的

#3


19  

Apologies for the necromancy but I'm able to specify hard-coded paths to globally installed modules:

对死灵的歉意,但我能指定硬编码的路径到全局安装的模块:

var pg = require("/usr/local/lib/node_modules/pg");

This isn't perfect but considering that Unity3d tries to "compile" all javascript that is included in the project directory I really can't install any packages.

这并不完美,但是考虑到Unity3d试图“编译”包含在项目目录中的所有javascript,我真的无法安装任何包。

#4


11  

You can use the package requireg to solve this problem:

你可以使用这个软件包来解决这个问题:

var forever = require('requireg')('forever')

will do the trick.

上大做文章。

Also, there's another module, global-npm, while specific to just using the global npm, you can look at the short code and see how the technique works.

另外,还有另一个模块,global-npm,而特定于只使用全局npm,您可以查看简短的代码,并了解该技术是如何工作的。

#5


11  

I know this is an old question, but I ran into this when trying to do some version checking using semver in a preinstall script in package.json. Since I knew I can't depend on any local modules installed, I used this to require semver from the global node_modules folder (as npm depends on it I know it's there):

我知道这是一个老问题,但是我在尝试在package.json的预安装脚本中使用semver进行版本检查时遇到了这个问题。由于我知道我不能依赖于安装的任何本地模块,所以我使用它来从全局node_modules文件夹中请求semver(当npm依赖于它时,我知道它在那里):

function requireGlobal(packageName) {
  var childProcess = require('child_process');
  var path = require('path');
  var fs = require('fs');

  var globalNodeModules = childProcess.execSync('npm root -g').toString().trim();
  var packageDir = path.join(globalNodeModules, packageName);
  if (!fs.existsSync(packageDir))
    packageDir = path.join(globalNodeModules, 'npm/node_modules', packageName); //find package required by old npm

  if (!fs.existsSync(packageDir))
    throw new Error('Cannot find global module \'' + packageName + '\'');

  var packageMeta = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json')).toString());
  var main = path.join(packageDir, packageMeta.main);

  return require(main);
}

I like this approach because this doesn't require the install of any special modules in order to use.

我喜欢这种方法,因为它不需要安装任何特殊模块来使用。

I didn't go with a NODE_PATH solution like others have suggested since I wanted to get this to work on anyone's machine, without having to require additional configuration/setup before running npm install for my project.

我没有像其他人建议的那样使用NODE_PATH解决方案,因为我想让它在任何人的机器上运行,而不需要在运行npm安装之前需要额外的配置/设置。

The way this is coded, it is only guaranteed to find top-level modules (installed using npm install -g ...) or modules required by npm (listed as dependencies here: https://github.com/npm/npm/blob/master/package.json). If you are using a newer version of NPM, it may find dependencies of other globally installed packages since there is a flatter structure for node_modules folders now.

这是编码的方式,它只保证找到*模块(使用npm安装-g…)或npm所需的模块(在这里列出的依赖项:https://github.com/npm/npm/blog/master/package.json)。如果您使用的是新版本的NPM,它可能会发现其他全局安装包的依赖关系,因为现在node_modules文件夹的结构比较扁平。

Hope this is useful to someone.

希望这对某人有用。

#6


5  

As per documentation, Node.js will search in the following locations by default:

按照文档节点。js将默认搜索以下地点:

  1. $HOME/.node_modules (global)

    $ HOME /。node_modules(全球)

    Note: $HOME is the user's home directory.

    注意:$HOME是用户的主目录。

  2. $HOME/.node_libraries (global)
  3. $ HOME /。node_libraries(全球)
  4. $PREFIX/lib/node (global)

    $前缀/ lib /节点(全球)

    Note: $PREFIX is Node.js's configured node_prefix.

    注意:$前缀是节点。js的node_prefix配置。

    To check the current value of node_prefix, run:

    要检查node_prefix的当前值,请运行:

    node -p process.config.variables.node_prefix
    

    Note: Prefix corresponds to --prefix param during build and it's relative to process.execPath. Not to confuse with value from the npm config get prefix command.source

    注意:在构建过程中,前缀对应于——前缀param,它相对于process.execPath。不要将npm配置的值与前缀进行混淆。

  5. Current node_modules folder. (local)

    当前node_modules文件夹。(本地)

  6. Path specified in the NODE_PATH environment variable.

    在NODE_PATH环境变量中指定的路径。

    Note: NODE_PATH environment variable is set to a colon-delimited list of absolute paths.

    注意:NODE_PATH环境变量被设置为一个冒号分隔的绝对路径列表。

If the given module can't be found, that means it is not present in one of the above locations.

如果无法找到给定的模块,这意味着它不在上述位置之一中。

Location of global root folder where modules are installed can be printed by: npm root -g (by default the path is computed at run-time unless overridden in npmrc file).

安装模块的全局根文件夹的位置可以通过:npm root -g(默认路径在运行时计算,除非在npmrc文件中被重写)来打印。

Solution

You can try the following workarounds:

你可以试试下面的方法:

  • Specify your global module location in NODE_PATH environment variable. E.g.

    在NODE_PATH环境变量中指定全局模块位置。如。

    echo 'require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node
    

    To test and print the value of NODE_PATH, run:

    要测试和打印NODE_PATH的值,请运行:

    echo 'console.log(process.env.NODE_PATH); require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node 
    
  • For more permanent solution, link your $HOME/.node_modules global user folder to point to the root folder, by running this command:

    为了更持久的解决方案,把你的$HOME链接起来。通过运行这个命令,node_modules全局用户文件夹指向根文件夹:

    ln -vs "$(npm root -g)" "$HOME"/.node_modules
    

    Then re-test it via: echo 'require("forever")' | node command.

    然后通过:echo 'require(“永远”)的|节点命令重新测试它。

  • Temporary change the current folder to where the extension has been installed globally, before invoking the script. E.g.

    在调用脚本之前,将当前文件夹临时更改为已在全局安装的位置。如。

    npm install -g forever
    cd "$(npm root -g)"
    echo 'require("forever")' | node
    cd -
    
  • Configure global installation destination in npm userconfig file (see: npm help 5 npmrc) or by userconfig param (--prefix).

    在npm userconfig文件中配置全局安装目的地(参见:npm帮助5 npmrc)或通过userconfig param(-前缀)。

    To display the current config, run: npm config list.

    要显示当前配置,请运行:npm配置列表。

    To edit the current config, run: npm config edit.

    要编辑当前配置,运行:npm配置编辑。

  • Specify the full path of node modules location when calling require(). E.g.

    在调用require()时指定节点模块位置的完整路径。如。

    require("/path/to/sub/module")
    
  • Install the package to custom location, e.g.

    将包安装到自定义位置,例如

    npm install forever -g --prefix "$HOME"/.node_modules
    

    However, the installation will go under ~/.node_modules/lib/node_modules/, so the location still needs to be added.

    然而,安装将会在~/。node_modules/lib/node_modules/,因此仍然需要添加位置。

    See: npm local install package to custom location

    查看:npm本地安装包到自定义位置。

  • Create a symlink in the current folder from the location of the global package. E.g.

    从全局包的位置创建当前文件夹中的symlink。如。

    npm link forever
    

#7


0  

You can put this line in your .profile file:

你可以把这条线放在你的.profile文件中:

export NODE_PATH="$(npm config get prefix)/lib/node_modules"

导出NODE_PATH="$(npm配置get前缀)/lib/node_modules"

This will make node use the global path.

这将使节点使用全局路径。

#1


148  

In Node.js, require doesn't look in the folder where global modules are installed.

在节点。js,要求不查看安装全局模块的文件夹。

You can fix this by setting the NODE_PATH environment variable. In Linux this will be:

您可以通过设置NODE_PATH环境变量来解决这个问题。在Linux中,这将是:

export NODE_PATH=/usr/lib/node_modules

Note: This depend on where your global modules are actually installed.

注意:这取决于您的全局模块实际安装在哪里。

See: Loading from the global folders.

参见:从全局文件夹加载。

#2


74  

After you install package globally you have to link the local project with global package

在全球安装包之后,必须将本地项目与全局包链接起来。

npm install express -g
cd ~/mynodeproject/
npm link express  

See here

在这里看到的

#3


19  

Apologies for the necromancy but I'm able to specify hard-coded paths to globally installed modules:

对死灵的歉意,但我能指定硬编码的路径到全局安装的模块:

var pg = require("/usr/local/lib/node_modules/pg");

This isn't perfect but considering that Unity3d tries to "compile" all javascript that is included in the project directory I really can't install any packages.

这并不完美,但是考虑到Unity3d试图“编译”包含在项目目录中的所有javascript,我真的无法安装任何包。

#4


11  

You can use the package requireg to solve this problem:

你可以使用这个软件包来解决这个问题:

var forever = require('requireg')('forever')

will do the trick.

上大做文章。

Also, there's another module, global-npm, while specific to just using the global npm, you can look at the short code and see how the technique works.

另外,还有另一个模块,global-npm,而特定于只使用全局npm,您可以查看简短的代码,并了解该技术是如何工作的。

#5


11  

I know this is an old question, but I ran into this when trying to do some version checking using semver in a preinstall script in package.json. Since I knew I can't depend on any local modules installed, I used this to require semver from the global node_modules folder (as npm depends on it I know it's there):

我知道这是一个老问题,但是我在尝试在package.json的预安装脚本中使用semver进行版本检查时遇到了这个问题。由于我知道我不能依赖于安装的任何本地模块,所以我使用它来从全局node_modules文件夹中请求semver(当npm依赖于它时,我知道它在那里):

function requireGlobal(packageName) {
  var childProcess = require('child_process');
  var path = require('path');
  var fs = require('fs');

  var globalNodeModules = childProcess.execSync('npm root -g').toString().trim();
  var packageDir = path.join(globalNodeModules, packageName);
  if (!fs.existsSync(packageDir))
    packageDir = path.join(globalNodeModules, 'npm/node_modules', packageName); //find package required by old npm

  if (!fs.existsSync(packageDir))
    throw new Error('Cannot find global module \'' + packageName + '\'');

  var packageMeta = JSON.parse(fs.readFileSync(path.join(packageDir, 'package.json')).toString());
  var main = path.join(packageDir, packageMeta.main);

  return require(main);
}

I like this approach because this doesn't require the install of any special modules in order to use.

我喜欢这种方法,因为它不需要安装任何特殊模块来使用。

I didn't go with a NODE_PATH solution like others have suggested since I wanted to get this to work on anyone's machine, without having to require additional configuration/setup before running npm install for my project.

我没有像其他人建议的那样使用NODE_PATH解决方案,因为我想让它在任何人的机器上运行,而不需要在运行npm安装之前需要额外的配置/设置。

The way this is coded, it is only guaranteed to find top-level modules (installed using npm install -g ...) or modules required by npm (listed as dependencies here: https://github.com/npm/npm/blob/master/package.json). If you are using a newer version of NPM, it may find dependencies of other globally installed packages since there is a flatter structure for node_modules folders now.

这是编码的方式,它只保证找到*模块(使用npm安装-g…)或npm所需的模块(在这里列出的依赖项:https://github.com/npm/npm/blog/master/package.json)。如果您使用的是新版本的NPM,它可能会发现其他全局安装包的依赖关系,因为现在node_modules文件夹的结构比较扁平。

Hope this is useful to someone.

希望这对某人有用。

#6


5  

As per documentation, Node.js will search in the following locations by default:

按照文档节点。js将默认搜索以下地点:

  1. $HOME/.node_modules (global)

    $ HOME /。node_modules(全球)

    Note: $HOME is the user's home directory.

    注意:$HOME是用户的主目录。

  2. $HOME/.node_libraries (global)
  3. $ HOME /。node_libraries(全球)
  4. $PREFIX/lib/node (global)

    $前缀/ lib /节点(全球)

    Note: $PREFIX is Node.js's configured node_prefix.

    注意:$前缀是节点。js的node_prefix配置。

    To check the current value of node_prefix, run:

    要检查node_prefix的当前值,请运行:

    node -p process.config.variables.node_prefix
    

    Note: Prefix corresponds to --prefix param during build and it's relative to process.execPath. Not to confuse with value from the npm config get prefix command.source

    注意:在构建过程中,前缀对应于——前缀param,它相对于process.execPath。不要将npm配置的值与前缀进行混淆。

  5. Current node_modules folder. (local)

    当前node_modules文件夹。(本地)

  6. Path specified in the NODE_PATH environment variable.

    在NODE_PATH环境变量中指定的路径。

    Note: NODE_PATH environment variable is set to a colon-delimited list of absolute paths.

    注意:NODE_PATH环境变量被设置为一个冒号分隔的绝对路径列表。

If the given module can't be found, that means it is not present in one of the above locations.

如果无法找到给定的模块,这意味着它不在上述位置之一中。

Location of global root folder where modules are installed can be printed by: npm root -g (by default the path is computed at run-time unless overridden in npmrc file).

安装模块的全局根文件夹的位置可以通过:npm root -g(默认路径在运行时计算,除非在npmrc文件中被重写)来打印。

Solution

You can try the following workarounds:

你可以试试下面的方法:

  • Specify your global module location in NODE_PATH environment variable. E.g.

    在NODE_PATH环境变量中指定全局模块位置。如。

    echo 'require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node
    

    To test and print the value of NODE_PATH, run:

    要测试和打印NODE_PATH的值,请运行:

    echo 'console.log(process.env.NODE_PATH); require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node 
    
  • For more permanent solution, link your $HOME/.node_modules global user folder to point to the root folder, by running this command:

    为了更持久的解决方案,把你的$HOME链接起来。通过运行这个命令,node_modules全局用户文件夹指向根文件夹:

    ln -vs "$(npm root -g)" "$HOME"/.node_modules
    

    Then re-test it via: echo 'require("forever")' | node command.

    然后通过:echo 'require(“永远”)的|节点命令重新测试它。

  • Temporary change the current folder to where the extension has been installed globally, before invoking the script. E.g.

    在调用脚本之前,将当前文件夹临时更改为已在全局安装的位置。如。

    npm install -g forever
    cd "$(npm root -g)"
    echo 'require("forever")' | node
    cd -
    
  • Configure global installation destination in npm userconfig file (see: npm help 5 npmrc) or by userconfig param (--prefix).

    在npm userconfig文件中配置全局安装目的地(参见:npm帮助5 npmrc)或通过userconfig param(-前缀)。

    To display the current config, run: npm config list.

    要显示当前配置,请运行:npm配置列表。

    To edit the current config, run: npm config edit.

    要编辑当前配置,运行:npm配置编辑。

  • Specify the full path of node modules location when calling require(). E.g.

    在调用require()时指定节点模块位置的完整路径。如。

    require("/path/to/sub/module")
    
  • Install the package to custom location, e.g.

    将包安装到自定义位置,例如

    npm install forever -g --prefix "$HOME"/.node_modules
    

    However, the installation will go under ~/.node_modules/lib/node_modules/, so the location still needs to be added.

    然而,安装将会在~/。node_modules/lib/node_modules/,因此仍然需要添加位置。

    See: npm local install package to custom location

    查看:npm本地安装包到自定义位置。

  • Create a symlink in the current folder from the location of the global package. E.g.

    从全局包的位置创建当前文件夹中的symlink。如。

    npm link forever
    

#7


0  

You can put this line in your .profile file:

你可以把这条线放在你的.profile文件中:

export NODE_PATH="$(npm config get prefix)/lib/node_modules"

导出NODE_PATH="$(npm配置get前缀)/lib/node_modules"

This will make node use the global path.

这将使节点使用全局路径。