我尝试全局安装,然后像这样使用forever和forever-monitor:

NPM install -g forever forever-monitor

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

我正在使用最新版本的node和npm,我已经知道了npm在全局vs本地安装中所做的改变,但我真的不想在每个项目上安装本地,我正在一个不支持链接的平台上工作,所以npm链接后的全局安装对我来说是不可能的。

我的问题是:为什么我不能要求全局安装包?这是一个功能还是一个漏洞?还是我做错了什么?

PS:只是让它非常清楚:我不想在本地安装。


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

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

在这里看到的


在Node.js中,require不会查找安装全局模块的文件夹。

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

export NODE_PATH=/usr/lib/node_modules

注意:这取决于全局模块实际安装的位置。

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


为死灵而道歉,但我能够指定全局安装模块的硬编码路径:

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

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


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

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

会成功的。

此外,还有另一个模块global-npm,虽然它是专门用于使用全局npm的,但你可以看看简短的代码,看看这项技术是如何工作的。


我知道这是一个老问题,但我在尝试在package.json中的preinstall脚本中使用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);
}

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

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

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

希望这对某些人有用。


根据文档,Node.js将默认搜索以下位置:

Path specified in the NODE_PATH environment variable. Note: NODE_PATH environment variable is set to a colon-delimited list of absolute paths. Current node_modules folder. (local) $HOME/.node_modules (global) Note: $HOME is the user's home directory. $HOME/.node_libraries (global) $PREFIX/lib/node (global) Note: $PREFIX is Node.js's configured node_prefix. To check the current value of node_prefix, run: 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

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

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

解决方案

您可以尝试以下变通方法:

Specify your global module location in NODE_PATH environment variable. E.g. echo 'require("forever")' | NODE_PATH="$(npm root -g):$NODE_PATH" node To test and print the value of NODE_PATH, run: 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: ln -vs "$(npm root -g)" "$HOME"/.node_modules Then re-test it via: echo 'require("forever")' | node command. 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). To display the current config, run: npm config list. To edit the current config, run: npm config edit. Specify the full path of node modules location when calling require(). E.g. 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. See: npm local install package to custom location Create a symlink in the current folder from the location of the global package. E.g. npm link forever


你可以把这一行放在你的.profile文件中:

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

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


对于依赖于大模块的CLI实用程序,比如puppeteer,我喜欢生成一个npm root -g,并使用它来要求全局模块。

try {
  const root = require('child_process').execSync('npm root -g').toString().trim()
  var puppeteer = require(root + '/puppeteer')
} catch (err) {
  console.error(`Install puppeteer globally first with: npm install -g puppeteer`)
  process.exit(1)
}

我试着按照其他答案,但对我有效的是

node_path = "C:\\Users\\{usename}\\AppData\\Roaming\\npm\\node_modules" Const *modulename* = require(node_path + "\\" +'*modulename*');


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

这将把您的本地项目文件夹链接到全局node_modules。