为嵌套文件夹运行npm install的最佳方法是什么?

在嵌套子文件夹中安装npm packages最正确的方法是什么?

 my-app /my-sub-module package.json package.json 

当在my-app运行npm install时,有什么方法可以自动npm install /my-sub-module中的软件包?

如果要运行一个命令在嵌套的子文件夹中安装npm包,可以在根目录下通过npm和main package.json运行脚本。 该脚本将访问每个子目录并运行npm install

下面是一个.js脚本,可以达到预期的效果:

 var fs = require('fs') var resolve = require('path').resolve var join = require('path').join var cp = require('child_process') // get library path var lib = resolve(__dirname, '../lib/') fs.readdirSync(lib) .forEach(function (mod) { var modPath = join(lib, mod) // ensure path has package.json if (!fs.existsSync(join(modPath, 'package.json'))) return // install folder cp.spawn('npm', ['i'], { env: process.env, cwd: modPath, stdio: 'inherit' }) }) 

请注意,这是一个从StrongLoop文章中取得的例子,专门介绍模块化的node.js项目结构(包括嵌套组件和package.json文件)。

正如所build议的,你也可以用bash脚本实现同样的function。

如果知道嵌套子目录的名称,我更喜欢使用安装后。 在package.json

 "scripts": { "postinstall": "cd nested_dir && npm install", ... } 

我的解决scheme非常相似。 纯粹的Node.js

将其作为“预安装”脚本运行

 const path = require('path') const fs = require('fs') const child_process = require('child_process') const root = process.cwd() npm_install_recursive(root) // Since this script is intended to be run as a "preinstall" command, // it will be `npm install` inside root in the end. console.log('===================================================================') console.log(`Performing "npm install" inside root folder`) console.log('===================================================================') function npm_install_recursive(folder) { const has_package_json = fs.existsSync(path.join(folder, 'package.json')) if (!has_package_json && path.basename(folder) !== 'code') { return } // Since this script is intended to be run as a "preinstall" command, // skip the root folder, because it will be `npm install`ed in the end. if (folder !== root && has_package_json) { console.log('===================================================================') console.log(`Performing "npm install" inside ${folder === root ? 'root folder' : './' + path.relative(root, folder)}`) console.log('===================================================================') npm_install(folder) } for (let subfolder of subfolders(folder)) { npm_install_recursive(subfolder) } } function npm_install(where) { child_process.execSync('npm install', { cwd: where, env: process.env, stdio: 'inherit' }) } function subfolders(folder) { return fs.readdirSync(folder) .filter(subfolder => fs.statSync(path.join(folder, subfolder)).isDirectory()) .filter(subfolder => subfolder !== 'node_modules' && subfolder[0] !== '.') .map(subfolder => path.join(folder, subfolder)) } 

添加Windoze支持snozza的答案

 var fs = require('fs') var resolve = require('path').resolve var join = require('path').join var cp = require('child_process') // get library path var lib = resolve(__dirname, '../lib/') fs.readdirSync(lib) .forEach(function (mod) { var modPath = join(lib, mod) // ensure path has package.json if (!fs.existsSync(join(modPath, 'package.json'))) return // Determine OS and set command accordingly const cmd = /^win/.test(process.platform) ? 'npm.cmd' : 'npm'; // install folder cp.spawn(cmd, ['i'], { env: process.env, cwd: modPath, stdio: 'inherit' }) })