[英]Is there a way to get npm install to install "bin" executables in the local node_modules directory (the .bin directory)?
[英]node.js / npm - override local default modules directory (node_modules)
假設我不想將我的本地(每個項目)軟件包安裝在node_modules中-我希望將其安裝在sources/node_modules
而不僅僅是node_modules
。 是否有可能像在涼亭中那樣覆蓋它?
在bower中,您可以為.bowerrc
文件提供directory
選項,請參閱docs 。 它的工作原理就好像您在bower_components locally
擁有bower_components locally
-沒有其他文件,加載程序,什么也沒有-只是容器目錄有所不同。
$ mkdir -p sources
$ ln -s package.json sources/package.json
$ npm install --prefix sources/
$ export NODE_PATH="`pwd`/sources"
您可以(如@adeneo所述)簡單地通過以下方式將軟件包安裝到任何文件夾:
$ npm install --prefix sources/ my-package
這將安裝到sources/my-package
。 但是,此解決方案在兩個方面遠非整潔:
聽起來,不是要安裝單個軟件包,而是要將package.json
中的所有內容都安裝到sources/
文件夾中。 問題在於,當您執行npm install --prefix sources/
,它還會在sources/
文件夾中尋找package.json
。 如果這對您有用,那就太好了。
我發現將package.json
保留在項目根目錄中並在其他位置安裝節點模塊的唯一方法是將本地package.json
符號鏈接到該目錄:
$ mkdir -p sources
$ ln -s package.json sources/package.json
$ npm install --prefix sources
如您所提到的,您可能希望腳本能夠需要類似於normal的模塊require('my-module')
而不是require('sources/my-module')
。
NODE_PATH
環境變量可以在這里提供幫助:
$ export NODE_PATH=`pwd`/sources/node_modules
$ node -e "require('my-module')" # Success
有一個重要的警告: node
將按特定順序查找模塊:
node_modules
文件夾中 node_modules
文件夾中,然后在其父目錄中。 NODE_PATH
路徑中,如果尚未找到該模塊 因此請注意,當前目錄或父目錄中沒有其他任何提到您的模塊的node_modules
文件夾,否則可能會造成混亂。
在您的根目錄中創建local_modules並添加package.json“ @ amazon / product-module”:“ file:local_modules / @ amazon / product-module”,
file structure
local_modules
@amazon (folder)
product-module (folder)
[add your file which overrides]
現在進行npm更新,您將把local_modules / @ amazon / product-module文件放入node_module / @ amazon / product-module
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.