我可以通过 package.json 在 node_modules 以外的单独文件夹中加载依赖项吗

can I load dependencies in a separate folder other than node_modules via package.json

当我 运行 时,我正在下载 node_modules 目录中的所有依赖项

{
  "name": "bootstrap-test-app",
  "engine": "node 5.5.0",
  "engineStrict": false,
  "version": "1.0.0",
  "description": "",
  "main": "master.js",
  "scripts": {},
  "author": "soum",
  "license": "BSD-2-Clause",
  "devDependencies": {},
  "dependencies": {
    "bootstrap-sass": "latest",
    "jquery": "3.1.1",
    "requirejs": "2.1.22"
  }
}

我的问题是可以为依赖项设置路径而不是 node_modules 例如,假设在一个名为 "dependencies" 的宿舍中?我可以设置目的地吗 属性 相对于项目的根,因此我的文件结构如下所示

my-app
  package.json
  dependencies
    -bootstrap-sass
    -jquery
    -requirejs
  node_modules

不,现在不可能,将来也不太可能。

查看 2013 年 GitHub 上的这个问题以及那里的评论:

可以用该讨论中的两句话来总结其未来:

  1. "Is there any way to change the location where the node_modules folder gets installed for local installed dependencies...??"
  2. "no, there is not, and there are no plans to implement such a feature."

另见 2011 年的一期旧刊:

你可以做的是创建一个你想要的目录和一个名为 node_modules 的符号链接,类似这样:

$ mkdir dependencies
$ ln -sv dependencies node_modules
‘node_modules’ -> ‘dependencies’
$ npm i tryjson -S
customdeps@1.0.0 /home/rsp/node/test/customdeps
└── tryjson@1.1.4 
$ ls dependencies
tryjson

我刚想到这个主意。我不确定 npm 是否可以安装在符号链接目录中,但我刚刚测试了它,事实证明它可以。

当然,要在 Node 中实际使用这些依赖项,您必须相应地设置 NODE_PATH 环境变量。参见: