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

查看:531
本文介绍了如何使用npm全局安装模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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标志,例如:

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

npm install forever -g

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

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

  • 如果您要安装要在程序中使用的内容,请使用 require('whatever'),然后安装 本地,位于您的根目录 项目.
  • 如果您要在外壳中安装要使用的东西,请在 命令行或其他内容,安装 全局,以便其二进制文件 最终进入您的PATH环境 变量.
  • 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.

但是,如果您想在全球范围内使用某些模块(即express或mongodb),请采纳此建议(也取自

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:

  • 将其安装在两个地方.认真地说,您的磁盘空间不足吗? 空间?很好,真的.他们很小 JavaScript程序.
  • 全局安装,然后npm link coffee-script或npm link express (如果您使用的平台支持 符号链接.)然后,您只需要 更新全局副本以更新所有 符号链接也是如此.
  • 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.

第一个选择是我最好的 观点.简单,清晰,明确.这 第二个真的很方便 将在一个库中重用相同的库 一堆不同的项目. (更多关于 npm链接在以后的文章中.)

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.

这篇关于如何使用npm全局安装模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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