如何在node_modules中本地化包的依赖关系

问题描述 投票:0回答:1

这是我项目的package.json

"dependencies": {
    ...
    "react-infinite-viewer": "^0.28.0"
}

此包的依赖项:

myproject -> react-infinite-viewer -> infinite-viewer -> gesto

项目的实际情况,直接或间接依赖于gesto的包其实不止一个。这意味着它是一个

multiple packages dependencies on gesto at different levels

我试过这个方法

"dependencies": {
  ...
  "react-infinite-viewer": "^0.28.0",
  "gesto":"file:packages/gesto"
}

此方法仅将修改后的代码安装在项目的node_modules中。这意味着我可以在我的项目中使用修改后的代码,但react-infinite-viewer仍将使用原始代码。

myproject
  ->node_modules
    ->react-infinite-viewer
        ->node_modules
            ->infinite-viewer
                ->node_modules
                    gesto // here is still use the original code
  ...
  ->gesto // here using the modified code 

我应该使用 pnpm 还是 lerna 来解决这个问题?

使用

yarn
/
pnpm i
/
yarn add xxx
...时,所有依赖于 gesto 的包都使用修改后的 gesto 代码

npm frontend node-modules pnpm
1个回答
0
投票

通过配置解决

resolutions

如果使用npm,请更改package.json:

{
    ...
      "scripts": {
          "preinstall": "npx npm-force-resolutions"
      },
     "resolutions": {
          "gesto":"file:packages/gesto"
     }
}

如果使用yarn,会比npm更容易

{
    ...
     "resolutions": {
          "gesto":"file:packages/gesto"
     }
}

Yarn 的选择性依赖解析功能是 npm-force-resolutions 的默认功能。您无需安装 npm-force-resolutions 即可使用它。 选择性依赖解决方案

© www.soinside.com 2019 - 2024. All rights reserved.