如何使用 npm 全局安装模块?

编程入门 行业动态 更新时间:2024-10-15 14:18:40
本文介绍了如何使用 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):

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

  • 如果您要安装要在程序中使用的东西,请使用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):

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

    当然,也有一些情况你想同时做.咖啡脚本和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 全局安装模块?

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

发布评论

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

>www.elefans.com

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