0

This is probably a total noob question about npm, but I'm wondering how I can extract the dependencies that would be installed when doing an install, for instance with dry-run.

The reasoning is that I want to know which packages are going to be installed, before actually performing the install.

In this way I can make sure that one particular package is avoided, should I want to define it so.

Slinger Jansen
  • 237
  • 2
  • 14
  • You can execute a script before to install that verify your dependencies. Look to this documentation https://docs.npmjs.com/cli/v6/using-npm/scripts – Jonathan Brizio Nov 06 '20 at 16:08

2 Answers2

1

You can use something like this,

Add a custom "install" script in your package.json file.

{
  "name": "custominstall",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "custom-install": "node custom-install"
  },
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "nodemon": "^2.0.6"
  }
}

Create custom-install.js in your root directory.

const { execSync } = require('child_process');

const packageJson = require('./package.json');

if (!packageJson.devDependencies.hasOwnProperty('nodemon')) {
    execSync(
        'npm install',
        {
            stdio: [
                0,
                1,
                2
            ]
        }
    );
} else {
    throw new Error('nodemon package should not be used in this project!');
}

This example code checks if nodemon package has been used as devDependency. Throws an error if it's used. Runs npm install and shows the command's output to you if it is not used.

You can customize it for your needs.

Use it like

npm run custom-install
Mansur
  • 1,622
  • 14
  • 27
  • Cool, I guess that would work for this scenario. I am surprised that there isn't something that can tell me which packages will be upgraded/installed, in a npm install -impact -dry-run kind of way. npm has the calculate the new dependency tree, why can't we see/diff it? – Slinger Jansen Nov 06 '20 at 12:29
1

You could Examine a package's dependency graph before you install it. By making installing rnp-remote-ls: https://www.npmjs.com/package/npm-remote-ls

First you install it the module gloablly:

npm install -g npm-remote-ls

Second you run:

npm-remote-ls <packagename>
Tom P
  • 26
  • 3
  • True, but then I want to diff it with the local dep tree. So I could get smth like: New packages to be installed: a-v1, b-v2, c-v1 Local packages to be updated: e-v1->e-v3 – Slinger Jansen Nov 06 '20 at 12:34