使用 npm 在本地和全局安装包有什么区别? [英] What is the difference between installing a package locally and globally using npm?

查看:71
本文介绍了使用 npm 在本地和全局安装包有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 npm 在本地和全局安装包有什么区别?

What is the difference between installing a package locally and globally using npm?

根据我的理解:

本地安装:npm install

Locally install: npm install <package>

  1. 此包/模块将在您的本地 node_modules 文件夹中找到,并且只能用于此项目.
  2. 可以使用 require("package") 访问此包/模块来自代码.
  3. 无法在命令行界面中访问此包/模块.

全局安装:npm install -g

Globally install: npm install <package> -g

  1. 这个包/模块会在你的机器上安装 node 的地方找到,比如/usr/local 并且可以在任何地方使用.
  2. 无法使用 require("package") 访问此包/模块来自代码.
  3. 可以在命令行界面中访问此包/模块.

请告诉我.如果我在这里误解了什么.谢谢!

Please let me know. If I could misunderstand anything here. Thanks!

推荐答案

你是对的,除了 1 点.

You are correct except for 1 point.

可以从命令行访问公开 CLI 实用程序的本地包.较新版本的 NPM 在本地 node_modules/ 中创建了这个 .bin/ 目录.
每当您尝试使用某个工具(以 babel 为例)时,如果您从命令行使用它并在您的项目中安装了它,npm 将正确识别该包并为您运行它的 CLI.

The local packages exposing CLI utilities can be accessed from the command line. Newer versions of NPM create this .bin/ directory inside the local node_modules/.
Whenever you try to use a tool (let's take babel for example), if you use it from the command line and you have it installed in your project, npm will properly identify that package and run it's CLI for you.

这是一篇关于该主题的有用文章.

Here's a useful article on the topic.

http://www.2ality.com/2016/01/locally-installed-npm-executables.html

这篇关于使用 npm 在本地和全局安装包有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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