簡體   English   中英

npm從父目錄安裝

[英]npm install from the parent directory

我有一個像這樣的目錄結構: /a/b/c

目錄c包含package.json ,應包含node_modules
如何我執行npm install的目錄里面a

我試過這樣的方法: npm --prefix b/c install b/c但是這樣,所有的符號鏈接都是直接在c里面創建的,而不是默認的node_modules/.bin

有沒有辦法實現這一目標?

節點:6.2.2
npm:3.10.2

使用預先在安裝鈎子的NPM package.json你內a目錄有可能在這種情況下的最佳選擇。

scripts: {
    preinstall: `cd b/c && npm install`
}

這樣在目錄a運行npm install也將執行c目錄安裝並提供無縫的開發體驗。

有點矯枉過正但可能有用......

在遞歸的幫助下,您可以找到node_modules

您可以在父目錄中運行此文件以查找node_modules中的node_modules並傳遞npm參數。

注意 :在Windows上測試過

 var child_process = require('child_process'); var fs = require('fs'); var path = require('path'); var safe = 0; let args = process.argv.splice(2).toString().replace(/,/g ,' '); function recurse(_path){ safe ++; if(safe > 5000){ console.log('directory may be too large') return } if(/node_modules$/.test(_path)){ let cwd = path.resolve(__dirname ,_path) console.log('found node_modules at '+cwd) child_process.exec(`start cmd.exe /k npm ${args}`,{cwd}) return } let directoryList = fs.readdirSync(_path); directoryList.forEach(function(nextDir){ if(fs.statSync(_path+'/'+nextDir).isFile()){ return } if(/^\\./.test(nextDir)){ //.folder beginging with . return } recurse(_path+'/'+nextDir); }) } recurse('./' ) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM