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

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

当npm install在my-app中运行时,在/my-sub-module中自动安装包的最佳方法是什么?


当前回答

要在每个子目录上运行npm install,你可以这样做:

"scripts": {
  ...
  "install:all": "for D in */; do npm install --cwd \"${D}\"; done"
}

在哪里

安装:所有只是脚本的名称,你可以随意命名

当前迭代的目录名

*/指定要查找子目录的位置。目录/*/将列出目录/内的所有目录,目录/*/*/将列出两层中的所有目录。

在指定文件夹中安装所有依赖项

你也可以运行一些命令,例如:

D在*/;&& npm install——cwd \"${D}\";完成

每次迭代都会打印“Installing stuff on your_subfolder/”。

这也适用于纱线

其他回答

添加Windows支持snozza的答案,以及跳过node_modules文件夹如果存在。

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 (!mod === 'node_modules' && !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' })
})

我的解决方案非常相似。 纯粹的node . js

下面的脚本检查所有子文件夹(递归地),只要它们有包。Json,并在每个文件中运行NPM install。 我们可以为它添加例外:允许没有package.json的文件夹。在下面的例子中,一个这样的文件夹是“packages”。 可以将其作为“预安装”脚本运行。

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 do `npm install` automatically inside the root folder in the end.
console.log('===================================================================')
console.log(`Performing "npm install" inside root folder`)
console.log('===================================================================')

// Recurses into a folder
function npm_install_recursive(folder)
{
    const has_package_json = fs.existsSync(path.join(folder, 'package.json'))

    // Abort if there's no `package.json` in this folder and it's not a "packages" folder
    if (!has_package_json && path.basename(folder) !== 'packages')
    {
        return
    }

    // If there is `package.json` in this folder then perform `npm install`.
    //
    // 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.
    // Hence the `folder !== root` condition.
    //
    if (has_package_json && folder !== root)
    {
        console.log('===================================================================')
        console.log(`Performing "npm install" inside ${folder === root ? 'root folder' : './' + path.relative(root, folder)}`)
        console.log('===================================================================')

        npm_install(folder)
    }

    // Recurse into subfolders
    for (let subfolder of subfolders(folder))
    {
        npm_install_recursive(subfolder)
    }
}

// Performs `npm install`
function npm_install(where)
{
    child_process.execSync('npm install', { cwd: where, env: process.env, stdio: 'inherit' })
}

// Lists subfolders in a folder
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))
}

正如fgblomqvist在评论中提到的,npm现在也支持工作区了。


有些答案相当古老。我认为现在我们有一些新的选择来建立单回购。

我建议使用纱线工作区:

工作区是一种设置包架构的新方法,从Yarn 1.0开始默认提供。它允许你设置多个包,这样你只需要运行一次yarn install就可以一次安装所有的包。

如果你更喜欢或不得不使用npm,我建议你看看lerna:

Lerna是一个工具,它优化了使用git和npm管理多包存储库的工作流。

Lerna也可以完美地使用纱线工作区。我刚刚完成了一个monorepo项目的设置-示例。

下面是一个配置为使用npm + lerna - MDC Web的多包项目的示例:他们使用package运行lerna引导。json是postinstall。

只是作为参考,以防人们遇到这个问题。你现在可以:

添加包。Json到子文件夹中 在main package.json中安装这个子文件夹作为reference-link:

NPM安装——保存路径/到/my/子文件夹

[macOS、Linux用户]:

我创建了一个bash文件来安装项目和嵌套文件夹中的所有依赖项。

find . -name node_modules -prune -o -name package.json -execdir npm install \;

解释:在根目录中,排除node_modules文件夹(即使在嵌套文件夹中),找到包含包的目录。Json文件,然后运行NPM install命令。

如果你只是想找到指定的文件夹(例如:abc123, def456文件夹),运行如下:

find ./abc123/* ./def456/* -name node_modules -prune -o -name package.json -execdir npm install \;