There may be a solution, I’m not sure that it will work as you expect. But you will have a direction to move forward.
package.json
{ "name": "express-demo", "version": "0.0.0", "private": true, "scripts": { "start": "node ./bin/www" }, "dependencies": { "cookie-parser": "~1.4.3", "debug": "~2.6.3", "express": "~4.15.2", "jade": "~1.11.0", "morgan": "~1.8.1", "serve-favicon": "~2.4.2", "webpack": "^3.8.1", "webpack-dev-middleware": "^1.12.0", "webpack-hot-middleware": "^2.20.0" }, "customDependecies": { "body-parser": [ "", "1.18.1", "1.18.0" ] } }
Note in the package.json file above, I added a new customDependecies key, which I will use to install several dependencies. Here I use the body-parser package for a demo. Then you need a file that can read this key and install the depilations.
INSTALL-deps.js
const {spawnSync} = require('child_process'); const fs = require('fs'); const customDependencies = require('./package.json').customDependecies; spawnSync('mkdir', ['./node_modules/.tmp']); for (var dependency in customDependencies) { customDependencies[dependency].forEach((version) => { console.log(`Installing ${dependency}@${version}`); if (version) { spawnSync('npm', ['install', `${dependency}@${version}`]); spawnSync('mv', [`./node_modules/${dependency}`, `./node_modules/.tmp/${dependency}@${version}`]); } else { spawnSync('npm', ['install', `${dependency}`]); spawnSync('mv', [`./node_modules/${dependency}`, `./node_modules/.tmp/${dependency}`]); } }); customDependencies[dependency].forEach((version) => { console.log(`Moving ${dependency}@${version}`); if (version) { spawnSync('mv', [`./node_modules/.tmp/${dependency}@${version}`, `./node_modules/${dependency}@${version}`]); } else { spawnSync('mv', [`./node_modules/.tmp/${dependency}`, `./node_modules/${dependency}`]); } }); } spawnSync('rm', ['-rf', './node_modules/.tmp']); console.log(`Installing Deps finished.`);
Here I install deps one by one in the tmp folder and after installation, I move them to the ./node_modules folder.
Once everything is installed, you can check the versions as shown below
index.js
var bodyParser = require('body-parser/package.json'); var bodyParser1181 = require('body-parser@1.18.1/package.json'); var bodyParser1182 = require('body-parser@1.18.0/package.json'); console.log(bodyParser.version); console.log(bodyParser1181.version); console.log(bodyParser1182.version);
Hope this serves your purpose.