如何使用npm全局安装模块?(How do I install a module globally using npm?)

编程入门 行业动态 更新时间:2024-10-10 06:17:36
如何使用npm全局安装模块?(How do I install a module globally using npm?)

我最近在OSX上安装了Node.js和npm模块,并且我认为设置有问题:

npm install [MODULE] is not installing the node.js module to the default path which is /usr/local/lib/node_modules.

I recently installed Node.js and npm module on OSX and have a problem with the settings I think:

npm install [MODULE] is not installing the node.js module to the default path which is /usr/local/lib/node_modules.

最满意答案

如果要在全局安装npm模块,请确保使用新的-g标志,例如:

npm install forever -g

关于从1.0rc开始的npm模块安装的一般建议(摘自blog.nodejs.org ):

如果您正在安装要在程序中使用的东西,请使用require('whatever'),然后在本地安装在项目的根目录。 如果要在shell中安装要使用的内容,请在命令行或其他内容中进行安装 ,以使其二进制文件最终在PATH环境变量中。

我刚刚使用这个建议,并且相当顺利地下降了。 我在全球安装了(因为它是一个命令行工具)和我的所有应用程序模块在本地。

但是,如果要全局使用某些模块(即express或mongodb),请采取以下建议(也可从blog.nodejs.org获取 ):

当然,在某些情况下,您要同时进行这两种操作。 Coffee脚本和Express都是具有命令行界面的应用程序的良好示例以及库。 在这种情况下,您可以执行以下操作之一:

安装在两个地方 。 认真的,你是磁盘空间短吗? 真的很好 他们是微小的JavaScript程序。 在全局安装,然后npm链接咖啡脚本或npm链接表达 (如果您在支持符号链接的平台上)。然后,您只需要更新全局副本以更新所有符号链接。

第一个选择是我认为最好的。 简单,清晰,明确。 如果你要在一堆不同的项目中重新使用相同的图书馆,那么第二个是非常方便的。 (更多关于未来分期付款中的npm链接)

我没有测试这些变体之一,但是它们似乎很简单。

If you want to install a npm module globally, make sure to use the new -g flag, for example:

npm install forever -g

The general recommendations concerning npm module installation since 1.0rc (taken from blog.nodejs.org):

If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project. If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.

I just recently used this recommendations and it went down pretty smoothly. I installed forever globally (since it is a command line tool) and all my application modules locally.

However, if you want to use some modules globally (i.e. express or mongodb), take this advice (also taken from blog.nodejs.org):

Of course, there are some cases where you want to do both. Coffee-script and Express both are good examples of apps that have a command line interface, as well as a library. In those cases, you can do one of the following:

Install it in both places. Seriously, are you that short on disk space? It’s fine, really. They’re tiny JavaScript programs. Install it globally, and then npm link coffee-script or npm link express (if you’re on a platform that supports symbolic links.) Then you only need to update the global copy to update all the symlinks as well.

The first option is the best in my opinion. Simple, clear, explicit. The second is really handy if you are going to re-use the same library in a bunch of different projects. (More on npm link in a future installment.)

I did not test one of those variations, but they seem to be pretty straightforward.

更多推荐

本文发布于:2023-08-07 11:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1464165.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用   全局   模块   npm   globally

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!