NPM Package Install Affected Dependencies List

144 Views Asked by At

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.

2

There are 2 best solutions below

1
On BEST ANSWER

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>
1
On

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