设置node.js REPL模块路径
我不知道如何添加 Node.js 安装路径(Mac OS X 上的 v.0.4.7)。我已经安装了 npm,并且它全局安装到 /usr/local/lib/node_modules
。然而,当我安装 npm 时,它没有通知 Node 它选择将全局模块放在哪里(应该有吗?)。现在,当我在命令行模式下使用 Node 时,我不能简单地 require()
我的全局安装的模块。所以,我想知道是否有某种 Node 配置文件或环境变量可以添加我的全局 npm 模块安装路径?
我知道当我在 Node 的命令行中时,我可以将其添加到 require.paths
数组中,但我想一劳永逸地添加这个全局模块文件夹,以便 Node 始终搜索当我从命令行 require()
模块时,该目录就是模块的目录。预先感谢您提供有关使 npm 和 Node 共存的任何帮助和指示!
I can't figure out how to add paths to my Node.js installation (v.0.4.7 on Mac OS X). I've installed npm, and it installs globally to /usr/local/lib/node_modules
. However, when I installed npm, it didn't notify Node about where it chose to put global modules (should it have?). Now when I use Node in command-line mode, I can't simply require()
my globally-installed modules. So, I'm wondering if there is some kind of Node configuration file or environment variable where I can add my global npm module installation path?
I know that I can just add it to the require.paths
array when I'm in Node's command line, but I want to add this global module folder once and for all, so that Node will always search that directory for modules when I require()
them from the command line. Thanks in advance for any help and pointers about making npm and Node co-exist!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好的,我明白了。结合来自 http://nodejs.org/docs/v0.4.7/api/modules.html 的信息#file_Modules 和 https://github.com/isaacs/npm/blob/master/doc/faq.md# readme 很明显,Node 在检查模块时会检查 NODE_PATH 环境变量。为了设置它,我做了以下操作:
这将 NODE_PATH 设置为 npm 的全局安装文件夹。
OK, I got it. Combining info from http://nodejs.org/docs/v0.4.7/api/modules.html#file_Modules and https://github.com/isaacs/npm/blob/master/doc/faq.md#readme it is clear that Node checks the NODE_PATH environment variable when checking for modules. To set this I did the following:
This sets NODE_PATH to npm's global install folder.
该死,我误会了。对此感到抱歉。
回到主题,您可以将这两行放入
set-repl-paths.js
然后执行
node set-repl-paths.js
您将得到一个带有路径的 repl已经设置了。您可以编写一个简单的 bash 脚本或设置一个 shell 别名,这样您就可以输入node-repl
或类似的内容。使用 npm 1.x,您应该使用本地安装,并为提供命令行实用程序的模块保留全局安装。如果您确实想要模块foo
进行全局安装,然后在你的模块文件夹中发出一个npm link foo
。现在您可以在模块中require("foo")
。最佳实践是使用本地安装。请参阅npm 1.0:全局与本地安装 .Damn, I misunderstood. Sorry about that.
Back in topic, you can put these two lines in
set-repl-paths.js
Then executing
node set-repl-paths.js
you will have a repl with the paths already setted. You can write a simple bash script or set a shell alias so you can just typenode-repl
or something similar.With npm 1.x you should use local installation, and leave global installation for modules that provide command line utilities.If you really want global install for modulefoo
, then in your module folder issue anpm link foo
. Now you canrequire("foo")
in your module.Best practice is to use local installation.See npm 1.0: Global vs Local installation on the nodejs blog.全局安装包(通常由命令行使用)
npm install --global PACKAGE_NAME
在我的例子中,我想将
jslint
安装为命令行工具。所以我运行了npm install --global jslint
这会将包安装到
/usr/local/lib/node_modules/
那么为什么会发生这一切呢?如果专门在项目中使用软件包,则不应全局安装它们。
欲了解更多信息,请查看帮助页面。
npm help install
npm help global
我还在
npm
常见问题解答To install a package globally (typically used by the command line)
npm install --global PACKAGE_NAME
In my case I wanted to install
jslint
as a command line tool. So I rannpm install --global jslint
This installs the package to
/usr/local/lib/node_modules/
So why all of this? You shouldn't be installing packages globally if they're being used specifically in a project.
For more information checkout the help pages.
npm help install
npm help global
I also found it in the
npm
FAQ对此的回复:
https://stackoverflow.com/a/5923898/7381355
您可以将其添加到 .bashrc 中,以便仅在以下情况下设置 NODE_PATH运行 repl 因为需要全局模块是一种反模式。
当没有参数传递给节点时,它设置 NODE_PATH。所以它不适用于像
node -i
这样的东西。您必须添加更多参数检查以涵盖运行 repl 的所有情况。这将使 repl 始终设置 NODE_PATH。如果您希望能够选择是否运行带有全局模块的 repl 或脚本,您可以将其添加到 .bashrc 中。
然后运行node_global即可。我选择了第二种选择。
In reply to this:
https://stackoverflow.com/a/5923898/7381355
You can add this to your .bashrc to only set NODE_PATH when running the repl since requiring global modules is an anti-pattern.
It sets NODE_PATH when no arguments are passed to node. So it wouldn't work with something like
node -i
. You would have to add more argument checking to cover all cases a repl is run.This would make the repl always set NODE_PATH. If you want to be able to choose whether to run the repl or a script with global modules or not, you can add this to your .bashrc instead.
Then just run node_global. I went with the second option.