I am building my app using separated micro services, each has its own repository and npm dependencies and might be on a diffrent server.
I am looking for a way to put all of the dependencies in one global package.json
file which can be accessed by url,
so in that way, I can update a shared dependency version, without running over all the different micro services, and update them one by one.
Is it possible?
Multiple package. json files give you a lot of flexibility to run different/incompatible versions of dependencies. As a practical example, on one of the projects that I work on we have 2 package. json files, one for the main application code and then another one for our BDD tests.
"dependencies" : Packages required by your application in production. "devDependencies" : Packages that are only needed for local development and testing.
Peer Dependencies: In package. json file, there is an object called as peerDependencies and it consists of all the packages that are exactly required in the project or to the person who is downloading and the version numbers should also be the same. That is the reason they were named as peerDependencies.
What if you create a new repository shared-dependencies
with a package.json
including all shared dependencies, then publishing it in npm.
After that you would be able to do npm i shared-dependencies
on each main project therefore you'll have access to all shared dependencies.
Unfortunately npm doesn't support specifying parent package.json file. Such feature was proposed some time ago, but npm maintainers come to conclusion that it should be achieved by external tools.
Of course you can write such tool yourself. There is one of possible aporaches:
Create library which contains package.json
file with all shared dependencies. In this example it will be called shared-deps
.
Create merge.js
script which adds shared dependencies to local package.json
file, and add it to shared-deps
library:
const fs = require('fs')
const localPackageJson = require('../../package.json')
const sharedPackageJson = require('./package.json')
Object.assign(localPackageJson.dependencies, sharedPackageJson.dependencies)
fs.writeFileSync('../../package.json', JSON.stringify(localPackageJson, null, 2))
Add to package.json
of app which will be using this shared dependencies following post-install hook:
"scripts": {
"postinstall": "node ./node_modules/a/merge.js"
}
Now, when you run npm install
, your shared dependencies will be installed together with shared-deps
library, and your package.json
will be updated afterwards.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With