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

查看:47
本文介绍了如何使用 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永远-g

自 1.0rc 以来关于 npm 模块安装的一般建议(取自 blog.nodejs.org):

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

  • 如果您要安装要在程序中使用的东西,请使用require('whatever'),然后安装它本地,在您的根项目.
  • 如果您要在 shell 中安装要使用的东西,请在命令行什么的,安装它是全局的,因此它的二进制文件最终进入您的 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),请采纳此建议(也取自 blog.nodejs.org):

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

当然,也有一些情况你想同时做.咖啡脚本和Express 都是很好的应用示例有一个命令行界面,如以及图书馆.在这些情况下,你可以执行以下操作之一:

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天全站免登陆