Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create shared package.json for multiple npm repositories

Tags:

node.js

npm

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?

like image 449
Or Bachar Avatar asked Sep 08 '16 07:09

Or Bachar


People also ask

Can you have more than one package json file?

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.

What is the difference between devDependencies and dependencies in package json?

"dependencies" : Packages required by your application in production. "devDependencies" : Packages that are only needed for local development and testing.

What is peerDependencies in package json?

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.


2 Answers

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.

like image 152
Nicolas Dominguez Avatar answered Oct 08 '22 10:10

Nicolas Dominguez


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:

  1. Create library which contains package.json file with all shared dependencies. In this example it will be called shared-deps.

  2. 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))
    
  3. 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"
    }
    
  4. 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.


like image 44
qzb Avatar answered Oct 08 '22 10:10

qzb