npm相当于纱线分辨率?

ada*_*lev 27 npm package.json yarnpkg

是否有相当于纱线分辨率功能的npm ?在npm package.json docs中没有提到它.

例如,我想在3.3.2安装lerna@3.3.2及其中一个依赖项(@ lerna/publish).目前使用像这样的纱线,但更喜欢使用npm而不是手动更改package-lock.json或类似的任何狡猾.

"devDependencies": {
  "lerna": "3.3.2",
},
"resolutions": {
  "@lerna/publish": "3.3.2"
}
Run Code Online (Sandbox Code Playgroud)

fer*_*sik 135

自 NPM 8.3 起,与纱线分辨率等效的称为overrides

为了确保无论您的依赖项依赖哪个版本,软件包 foo 始终安装为版本 1.0.0:

{
   "overrides": {
     "foo": "1.0.0"
   }
 }
Run Code Online (Sandbox Code Playgroud)

文档:https://docs.npmjs.com/cli/v9/configuring-npm/package-json#overrides

添加该功能的原因和方式已在RFC中进行了讨论,并且有关该主题的讨论已在此 github问题上建立了索引


Gaj*_*jus 25

是由于 support overrides,相当于 yarn 的resolutions.

有关当前 RFC 状态的更多信息:

https://github.com/npm/rfcs/blob/latest/accepted/0036-overrides.md


Jul*_*ien 20

npm本身似乎不支持这个,但是这个包旨在添加这个功能:

https://github.com/rogeriochaves/npm-force-resolutions

  • @Gajus:[此提交消息](https://github.com/rogeriochaves/npm-force-resolutions/commit/68cc61f3f1594fc2875b6092a409d25ebada3b52)建议它现在应该在npm 7上工作。`npm-force-resolutions`的相应版本是`0.0.10`。 (4认同)
  • 2022 年,**受支持**:/sf/answers/4717858771/ (2认同)

Gaj*_*jus 10

据我所知,npm-force-resolutions不适用于 v7。该package-lock.json格式V7改变npm-force-resolutions不再更新相关的领域。

然而,编写一个脚本来限制你的依赖树到一个包的只有 1 个版本是相对容易的,例如

#!/usr/bin/env node

/* eslint-disable unicorn/prevent-abbreviations */
/* eslint-disable import/unambiguous */
/* eslint-disable import/no-commonjs */
/* eslint-disable node/shebang */

const fs = require('fs').promises;
const path = require('path');

const main = async (resolutions) => {
  const packageLockFilePath = path.resolve(__dirname, '../package-lock.json');

  for (const [name, version] of Object.entries(resolutions)) {
    const packageLock = JSON.parse(await fs.readFile(packageLockFilePath));

    const packagePaths = Object.keys(packageLock.packages);

    const deletePaths = [];

    for (const packagePath of packagePaths) {
      if (packagePath.endsWith('/' + name)) {
        if (packageLock.packages[packagePath].version !== version) {
          deletePaths.push(packagePath);
        }
      }
    }

    for (const packagePath of deletePaths) {
      for (const deletePath of deletePaths) {
        if (packagePath === deletePath || packagePath.startsWith(deletePath + '/')) {
          // eslint-disable-next-line fp/no-delete
          delete packageLock.packages[packagePath];
        }
      }
    }

    await fs.writeFile(
      packageLockFilePath,
      JSON.stringify(packageLock, null, '  '),
    );
  }
};

main(require('../package.json').resolutions);

Run Code Online (Sandbox Code Playgroud)

此脚本只是删除所有不满足 中resolutions定义的依赖项的链接package.json

要执行脚本,只需将其添加到package.json scripts并定义resolutions字段,例如

{
  "scripts": {
    "postinstall": "node bin/fix-package-lock.js"
  },
  "resolutions": {
    "webpack": "5.6.0"
  }
}

Run Code Online (Sandbox Code Playgroud)

resolutions仅仅是地图包的名称和应保持在依赖关系树这些软件包的确切版本,即上述配置将删除的所有版本不在5.6.0。只要你安装webpack@5.6.0version 作为你正在使用的项目的依赖项,这将保证所有包加载相同版本的 webpack。


小智 7

对于阅读本文的任何人来说,自:party:版本起,覆盖现在已成为 npm 支持的功能。8.3.0