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

标签 javascript npm module dependencies

我有以下问题。目前我想构建一个 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/

相关文章:

javascript - 以更短的方式使用事件

node.js - aws lambda 上的 json 模式验证

javascript - 在 Javascript 中设置模块的原型(prototype)

import - 不要在 coq 中导入符号

php - 在html链接中调用javascript函数

javascript - 如何通过搜索栏 React Native 搜索 FlatList?

Javascript 基本执行顺序

node.js - 安装 Angular deps 时,NPM 安装导致 404

ios - 未定义不是对象(评估 RNRandomBytes.seed)

c++ - 与 snprintf 和 size_t len 混淆