node.js - npm 在当前目录安装模块

标签 node.js npm

当我运行时:

npm install my-app

应用安装到 node_modules/my-app/...

我也试过

npm install -g my-app

但这也不起作用。

如何将模块直接安装到当前目录?

最佳答案

npm install 安装包locally or globally :

  • 本地:npm 在当前目录中查找名为 node_modules 的现有文件夹,并为您在该文件夹中安装的每个包创建一个文件夹.如果它在此处找不到现有的 node_modules 文件夹,则会查看当前目录的祖先,直到找到。如果找不到,则会在当前目录中创建一个。
  • 全局:如果您使用 -g(全局)选项,则软件包安装在全局位置。这个位置因 Linux 发行版而异,但 /usr/local/lib/node_modules/packagename 就是一个例子。 CentOS7 使用 /usr/lib/node_modules/packagename

只有当包是你想用作命令的东西时,你才应该使用 -g

Just like how global variables are kind of gross, but also necessary in some cases, global packages are important, but best avoided if not needed.

In general, the rule of thumb is:

  1. 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.
  2. 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.

npm 不会将包的文件直接安装到当前目录中。

不过,这其实是件好事。它将依赖项的文件与您的应用程序分开,并且 Node automatically searches the node_modules folder when you require something .

关于node.js - npm 在当前目录安装模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14032160/

相关文章:

node.js - npm 安装错误为 errno -2

node.js - 创建一个属于另外两个对象的对象

javascript - 从字符串中删除字符

javascript - 简单的游戏循环 Socket.io + Node.js + Express

node.js - Node 从扩展名获取文件描述

node.js - NPM 本周/月下载次数最多的资源

node.js - 使用另一个 Node 版本重新编译 Node 依赖项

node.js - Jasmine 无法监视 async.waterfall block 中调用的函数

node.js - 使用 npm 管理客户端脚本

node.js - 安装 http-server : No matching version found for ecstatic@^3. 0.0 时出错