4

Simple question : Is it possible, in a package.json, to reference another package.json, and install its dependencies ? Thank you.

2 Answers 2

3

Yes, this is possible, and this is automatically done by npm install.

If you have pkg-a that depends on pkg-b, including pkg-a in your dependencies will install both pkg-a and pkg-b when running npm install. That is because dependencies are actually references to the package.json of other packages. NPM, upon running install, builds a dependency tree of all the packages that are indirectly required by your current project, and installs all of them in the node_modules directory, and keeps track of them all in package-lock.json.

Sign up to request clarification or add additional context in comments.

4 Comments

In my case, the pkg-b is local, so how can I reference it in my dependencies ? Thank you
"pkg-a": "file:./path/to/it" instead of "pkg-a": "version". More details here
I tried that, but I don't know if you understood what I want. The "pkg-a": "file:./path/to/it" copies the content of the local "pkg-a", ok. But in that pkg-a, there is another package.json, containing dependencies. I want these dependencies to install too, when I run the npm install. Actually, this is not the case with "file:./path/to/it"
This answer does not actually solve the requested problem, namely to import dependencies from one local package.json into another so that running npm install will install the dependencies of the 'sub' package. From the docs: "note: Packages linked by local path will not have their own dependencies installed when npm install is ran in this case. You must run npm install from inside the local path itself."
-1

Good question! but this is not possible as you cannot internally reference one json document from another (json is just a document format, it lacks any ability to process logic, import files etc), npm is configured to run using a single package.json file so your best best would be to put all your dependencies in a single package.json file or split your project into two directories with two separate package.json files, two npm installs etc, if for some reason you require your dependencies to be separate. You could then run your two node projects separately and connect via http if you wish.

The only way that you could come close to doing this would be to write an npm start script in the package.json that cds to another directory with a package.json and runs npm install, this would however only install the dependencies in the second directory node-modules/ folder

1 Comment

This is not true see the above answer from Nino

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.