Print a list of all installed node.js modules

Javascriptnode.jsNpmPackageDependencies

Javascript Problem Overview


In a node.js script that I'm working on, I want to print all node.js modules (installed using npm) to the command line. How can I do this?

console.log(__filename);

//now I want to print all installed modules to the command line. How can I do this?

Javascript Solutions


Solution 1 - Javascript

If you are only interested in the packages installed globally without the full TREE then:

npm -g ls --depth=0

or locally (omit -g) :

npm ls --depth=0

Solution 2 - Javascript

Use npm ls (there is even json output)

From the script:

test.js:

function npmls(cb) {
  require('child_process').exec('npm ls --json', function(err, stdout, stderr) {
    if (err) return cb(err)
    cb(null, JSON.parse(stdout));
  });
}
npmls(console.log);

run:

> node test.js
null { name: 'x11', version: '0.0.11' }


Solution 3 - Javascript

list of all globally installed third party modules, write in console:

 npm -g ls

Solution 4 - Javascript

in any os

npm -g list

and thats it

Solution 5 - Javascript

Generally, there are two ways to list out installed packages - through the Command Line Interface (CLI) or in your application using the API.

Both commands will print to stdout all the versions of packages that are installed, as well as their dependencies, in a tree-structure.


CLI
npm list

Use the -g (global) flag to list out all globally-installed packages. Use the --depth=0 flag to list out only the top packages and not their dependencies.


API

In your case, you want to run this within your script, so you'd need to use the API. From the docs:

npm.commands.ls(args, [silent,] callback)

In addition to printing to stdout, the data will also be passed into the callback.

Solution 6 - Javascript

Why not grab them from dependencies in package.json?

Of course, this will only give you the ones you actually saved, but you should be doing that anyway.

console.log(Object.keys(require('./package.json').dependencies));

Solution 7 - Javascript

for package in `sudo npm -g ls --depth=0 --parseable`; do
    printf "${package##*/}\n";
done

Solution 8 - Javascript

As the end of 2021, there are few obvious way to do it, and a part as the only one give on the answer above this is a complete list.

The Node.js Documentation is actually pretty well explained regarding the matter, this is a collective list of the main commands.

All Commands will run the list of installed modules Locally. In order to run global level just add a -g flag at the end of the statement.

  1. See the version of all installed npm packages, including their dependencies.

    ❯ npm list
    
     >>> /Users/joe/dev/node/cowsay
     └─┬ cowsay@1.3.1
       ├── get-stdin@5.0.1
       ├─┬ optimist@0.6.1
       │ ├── minimist@0.0.10
       │ └── wordwrap@0.0.3
       ├─┬ string-width@2.1.1
       │ ├── is-fullwidth-code-point@2.0.0
       │ └─┬ strip-ansi@4.0.0
       │   └── ansi-regex@3.0.0
       └── strip-eof@1.0.0
    
  2. Get only your top-level packages

    npm list --depth=0
    
  3. Get the version of a specific package by specifying its name.

    npm list <package-name>
    
  4. See what's the latest available version of the package on the npm repository

    npm view <package-name> version
    
  5. Install an old version of an npm package using the @ syntax

    npm install @ npm install [email protected]

    Global package

    npm install -g [email protected]

  6. Listing all the previous versions of a package

    npm view cowsay versions
    [ '1.0.0',  '1.0.1',  '1.0.2',  '1.0.3',  '1.1.0',  '1.1.1',  '1.1.2',  '1.1.3',  ....]
    

Update all the Node.js dependencies

  1. Install new minor or patch release

     npm update
    
  2. Install new minor or patch release but not update package.json

     npm update --no-save
    
  3. To discover new releases of the packages, this gives you the list of a few outdated packages in one repository that wasn't updated for quite a while

      npm outdated
    

> Some of those updates are major releases. Running npm update won't update the version of those. Major releases are never updated in this way because they (by definition) introduce breaking changes, and npm wants to save you trouble.

To update all packages to a new major version, install the npm-check-updates package globally:

npm install -g npm-check-updates
ncu -u

> This will upgrade all the version hints in the package.json file, to dependencies and devDependencies, so npm can install the new major version


Dev Dependency

Install in development dependencies.

npm install <package-name> -D
npm install <package-name> --save-dev # same as above

Avoid installing those development dependencies in Production with

npm install --production

Uninstalling npm packages

npm uninstall <package-name>
npm uninstall -g <package-name> # globally uninstall

10. Uninstall a package and ** remove the reference in the package.json**

      npm uninstall <package-name> -S
      npm uninstall <package-name> --save # same as above

Some commands with global flag examples.

npm list -g 
npm list --depth=0 -g
npm list <package-name> -g 
npm view <package-name> version -g 

Additional Commands

Documentation

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAnderson GreenView Question on Stackoverflow
Solution 1 - JavascriptanistonView Answer on Stackoverflow
Solution 2 - JavascriptAndrey SidorovView Answer on Stackoverflow
Solution 3 - JavascripthfarazmView Answer on Stackoverflow
Solution 4 - JavascriptMuhammad F. MusadView Answer on Stackoverflow
Solution 5 - Javascriptd4nyllView Answer on Stackoverflow
Solution 6 - JavascriptneojpView Answer on Stackoverflow
Solution 7 - JavascriptA TView Answer on Stackoverflow
Solution 8 - JavascriptFederico BaùView Answer on Stackoverflow