如何使用 npm 在当前目录中安装 package.json 依赖项 [英] How do I install package.json dependencies in the current directory using npm

查看:70
本文介绍了如何使用 npm 在当前目录中安装 package.json 依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个网络应用:fooapp.我在根目录中有一个 package.json.我想在特定的 node_modules 目录 中安装所有依赖项.我该怎么做?

I have a web app: fooapp. I have a package.json in the root. I want to install all the dependencies in a specific node_modules directory. How do I do this?

假设我有两个 widget 依赖项.我想以这样的目录结构结束:

Lets say I have two widget dependencies. I want to end up with a directory structure like this:

node_modules/
  widgetA
  widgetB
fooapp/
  package.js
  lib
  ..

我得到了什么

当我运行 npm install fooapp/ 我得到这个:

node_modules/
  fooapp/
    node_modules/
      widgetA
      widgetB
    package.js
    lib/
    ..
fooapp/
  package.js
  lib/
  ..

npm 在 node_modules 目录中复制我的应用程序目录,并将软件包安装在 另一个 node_modules 目录中.

npm makes a copy of my app directory in the node_modules dir and installs the packages inside another node_modules directory.

我理解这对于安装软件包很有意义.但是我没有 require() 我的网络应用程序在别的东西里面,我直接运行它.我正在寻找一种简单的方法来将我的依赖项安装到特定的 node_modules 目录中.

I understand this makes sense for installing a package. But I don't require() my web app inside of something else, I run it directly. I'm looking for a simple way to install my dependencies into a specific node_modules directory.

推荐答案

运行:

npm install

从您的应用程序目录中(即 package.json 所在的位置)为您的应用程序安装依赖项,而不是将其安装为模块,如此处所述.这些将放置在相对于您的 package.json 文件的 ./node_modules 中(它实际上比这稍微复杂一些,因此请查看 npm 文档在这里).

from inside your app directory (i.e. where package.json is located) will install the dependencies for your app, rather than install it as a module, as described here. These will be placed in ./node_modules relative to your package.json file (it's actually slightly more complex than this, so check the npm docs here).

如果需要,您可以自由地将 node_modules 目录移动到应用程序的父目录,因为节点的要求"机制理解这一点.但是,如果您想通过安装/更新来更新应用的依赖项,npm 将不会看到重新定位的node_modules",而是会创建一个新目录,同样是相对于 package.json.

You are free to move the node_modules dir to the parent dir of your app if you want, because node's 'require' mechanism understands this. However, if you want to update your app's dependencies with install/update, npm will not see the relocated 'node_modules' and will instead create a new dir, again relative to package.json.

为了防止这种情况,只需从您的应用程序目录创建一个指向重新定位的 node_modules 的符号链接:

To prevent this, just create a symlink to the relocated node_modules from your app dir:

ln -s ../node_modules node_modules

这篇关于如何使用 npm 在当前目录中安装 package.json 依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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