简体   繁体   English

将node_modules安装到供应商

[英]Install node_modules to vendor

How can I install npm modules locally for each project to vendor/node_modules and make package.json file see them. 如何在本地为每个项目安装npm模块到vendor/node_modules ,并使package.json文件看到它们。

I don't want to move package.json to vendor folder 我不想将package.json移动到vendor文件夹

I have bower and in .bowerrc I specify the bower_components path - that is super easy. 我有凉亭和.bowerrc我指定了bower_components路径 - 这非常简单。

How can I do that with npm ? 我怎么能用npm做到这一点?

I`ve read the docs, npmrc docs, some questions here, googled, wasted more than an hour - still no luck. 我已经阅读了文档,npmrc文档,这里的一些问题,Google搜索,浪费了一个多小时 - 仍然没有运气。 This is ridiculously hard for such an easy task. 这对于如此简单的任务来说非常困难。

I don't care about minuses, just tell me how to do that finally. 我不关心弊端,最后告诉我该怎么做。

Frustrated by the fact that there seems to be no built in way to install into a node_modules folder in an arbitrary subfolder, I came up with a sneaky solution using the two following scripts: 由于似乎没有内置的方法安装到任意子文件夹中的node_modules文件夹这一事实让我感到沮丧,因此我想出了一个使用以下两个脚本的偷偷摸摸的解决方案:

preinstall.js preinstall.js

var fs = require("fs");
try
{
    fs.mkdirSync("./app/node_modules/");
}
catch(e)
{
}

try
{
    if(process.platform.indexOf("win32") !== -1)
    {
        fs.symlinkSync("./app/node_modules/","./node_modules/","junction");
    }
    else
    {
        fs.symlinkSync("./app/node_modules/","./node_modules","dir");
    }
}
catch(e){}

postinstall.js postinstall.js

var fs = require("fs");
try
{
    if(process.platform.indexOf("win32") !== -1)
    {
        fs.unlinkSync("./node_modules/");
    }
    else
    {
        fs.unlinkSync("./node_modules");
    }
}
catch(e){}

All you need to do is use them in your package.json file by adding them to the scripts option: 您需要做的就是将它们添加到scripts选项中,在package.json文件中使用它们:

"scripts": {
    "preinstall": "node preinstall.js",
    "postinstall": "node postinstall.js"
},

So, the big question is: what does it do? 所以,最大的问题是:它做了什么?

  1. Well, when you call npm install the preinstall.js script fires which creates a node_modules in the subfolder you want. 好吧,当你调用npm installpreinstall.js脚本将触发,这会在你想要的子文件夹中创建一个node_modules Then it creates a symlink or ( shortcut in Windows) from the node_modules that npm expects to the real node_modules . 然后它从npm期望的node_modules创建一个symlink或(Windows中的shortcut )到真实的node_modules

  2. Then npm installs all the dependencies. 然后npm安装所有依赖项。

  3. Finally, once all the dependencies are installed, the postinstall.js script fires which removes the symlink ! 最后,一旦安装了所有依赖项,就会触发postinstall.js脚本,删除symlink

Here's a handy gist with all that you need. 这是一个方便的要点 ,你需要的一切。

You cannot, not using built-in npm functionality. 你不能,不使用内置的npm功能。

This discussion on the npm github repository covers the issue. 关于npm github存储库的讨论涵盖了该问题。 It's also being addressed in this answer which is part of their FAQ. 这个问题也在他们的常见问题解答中得到解决。

You can still do the installs "manually" by copying modules into your /vendor directory and then calling them using the require("./vendor/whatever") syntax...but that means each require needs to use your new custom location. 您仍然可以通过将模块复制到/vendor目录中然后使用require("./vendor/whatever")语法调用它们来“手动”进行安装......但这意味着每个require使用新的自定义位置。 There are a few ways you could handle this but they all mean you are doing extra work in your source to accomodate the custom location. 有几种方法可以解决这个问题,但它们都意味着您在源中做了额外的工作来容纳自定义位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM