use npm with different configuration than package.json

Viewed 10168

I have a complex production environment driven by package.json.

The problem: I wish to install locally some additional packages, keep an eye on the list and versions of them.

Solution (how to get there): point npm to use another config file, excluded from git, which would keep my private dependencies. Use the file to add packages to local node_modules with npm install. So actually all I need is to change configuration context of npm.

I have no clue how to point npm to use a different config (something like gulp -gulpfile).

Update from comments The dev-dependencies is not the way to go. I use stuff 90% of other developers do not need to get installed in their node_modules (in fact I could break their environment in a strange way updating dev-dependencies in git-shared core project-wide package.json).

4 Answers

You can write your own realisation.

You need to write package.json builder that can generate package.json where dependencies will be only needed

USAGE

node package.json.builder.js prod+test
npm install

Here will be created package.json file with dependencies that described in package.dependencies.js.

REALISATION

1.Add package.json to .gitignore

2.Create file package.json.builder.js will create package.json file

let fs = require('fs');
let pkgDep = require('./package.dependencies.js');

let pkgTemplete = fs.readFileSync('./package.json.hbs', "utf8");
let obj = {}, dep = [];

if (process.argv[2]) {
  let array = process.argv[2].split('+');

  for (let i = 0; i < array.length; i++) {
    if (!pkgDep[array[i] + 'Dependencies']) {
      console.error(array[i] + 'Dependencies' + ' not found in package.dependencies.js');
    } else {
      dep.push(array[i] + 'Dependencies');
    }

Object.assign(obj, pkgDep[array[i] + 'Dependencies'] || {});
  }
}

pkgTemplete = pkgTemplete.replace('{{dependencies}}', JSON.stringify(obj));

fs.writeFileSync('./package.json', pkgTemplete);
console.log('package.json created with ' + dep.join('+'));

3.Create package.json.hbs (package.json without dependencies)

{
  "name": "",
  "version": "1.0.0",
  "dependencies": {{dependencies}},
  "scripts": {
  }
}

4.Create file package.dependencies.js object that contains only list of packages and its versions

module.exports = {
  prodDependencies: {
    "babel-core": "6.26.0"
  },
  devDependencies: {
    "core-js": "2.5.5"
  },
  testDependencies: {
    "jest": "23.5.0"
  }
}
Related