简体   繁体   中英

How import module dependencies from other npm dependency?

I have the following question. Currently I want to build a NPM module which this expected behaviour:

Once I've installed it, all its dependencies would work perfectly in the project only import them.

First of all, what I have is this:

在此处输入图片说明

I have "my-module" project as the dependencies modules I want with that specific versions.

On the other hand I have a project which imports it as we can see.

Inside my project I want to write, for example, the following code and finally run it:

import {Calendar} from 'primereact/calendar';

How could I deal with this trouble?

Thanks in advance!

EDIT: My project's package.json is this

{
  "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"
  }
}

Also my-module's 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"
  }
}

Both packages will need the primereact dependency, when you do an install on the top level package it will move the primereact dependency up, assuming there's a version that can satisfy both dependencies.

Have a look at 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.

You can also run npm dedupe

In node.js, it is recommended that you declare those dependencies specifically as a part of your app.

Require dependency of another dependency in node modules

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM