NodeJS 需要一个全局模块/包 [英] NodeJS require a global module/package

查看:29
本文介绍了NodeJS 需要一个全局模块/包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试全局安装,然后像这样使用 foreverforever-monitor:

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

npm install -g 永远永远监控

我看到了通常的输出以及将文件复制到全局路径的操作,但是如果我尝试 require("forever"); 我收到一个错误,说模块不是没有找到.

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.

我正在使用 node 和 npm 的最新版本,我已经知道 npm 在全局和本地安装中所做的更改,但我真的不想在每个项目上安装 localy并且我在一个不支持 link 的平台上工作,所以在全局安装后我无法使用 npm link.

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.

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

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:为了清楚说明:我不想在本地安装.

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

推荐答案

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

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

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

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

export NODE_PATH=/usr/lib/node_modules

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

请参阅:从全局文件夹加载.

这篇关于NodeJS 需要一个全局模块/包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆