gpt4 book ai didi

javascript - 如何从其他 npm 依赖项导入模块依赖项?

转载 作者:行者123 更新时间:2023-12-02 23:11:01 24 4
gpt4 key购买 nike

我有以下问题。目前我想构建一个 NPM 模块,其预期行为:

一旦我安装了它,它的所有依赖项都可以在项目中完美运行,只需导入它们。

首先,我拥有的是:

enter image description here

我有“my-module”项目作为我想要的特定版本的依赖模块。

另一方面,我有一个项目导入它,正如我们所看到的。

在我的项目中,我想编写以下代码并最终运行它:

import {Calendar} from 'primereact/calendar';

我该如何处理这个麻烦?

提前致谢!

编辑:我的项目的 package.json 是这样的

{
"name": "my-project",
"version": "0.0.0",
"description": "",
"main": "index.js",
"scripts": {
"dev": "next src",
"build": "next build src",
"start": "next start src"
},
"license": "ISC",
"dependencies": {
"next": "^9.0.3",
"react": "^16.8.6",
"react-dom": "^16.8.6",
"@ascope/my-module": "file://../my-module"
}
}

还有我的模块的 package.json:

{
"name": "my-module",
"version": "0.0.1",
"main": "index.js",
"scripts": {
"test": "exit 1"
},
"license": "ISC",
"bundledDependencies": [
"primereact",
"rxjs"
],
"dependencies": {
"primereact": "^3.1.8",
"rxjs": "^6.5.2"
},
"peerDependencies": {
"primereact": "^3.1.8",
"rxjs": "^6.5.2"
}
}

最佳答案

两个包都需要 primereact 依赖项,当您在顶级包上执行 install 时,它会将 primereact 依赖项向上移动,假设有一个版本可以满足这两个依赖项。

看看NPM install algorithm

For this package{dep} structure: A{B,C}, B{C}, C{D}, this algorithm produces:

A
+-- B
+-- C
+-- D

That is, the dependency from B to C is satisfied by the fact that A already caused C to be installed at a higher level. D is still installed at the top level because nothing conflicts with it.

<小时/>

For A{B,C}, B{C,D@1}, C{D@2}, this algorithm produces:

A
+-- B
+-- C
`-- D@2
+-- D@1

Because B’s D@1 will be installed in the top level, C now has to install D@2 privately for itself. This algorithm is deterministic, but different trees may be produced if two dependencies are requested for installation in a different order.

您还可以运行npm dedupe

关于javascript - 如何从其他 npm 依赖项导入模块依赖项?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57361623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com