154

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? 
3
  • In this case, it might be useful to write a function that returns the path of the node.js modules folder (if that's possible). A cross-platform solution would be ideal. Commented Dec 20, 2012 at 23:05
  • Information about getting the list of files from a directory: stackoverflow.com/questions/2727167/… Commented Dec 20, 2012 at 23:07
  • 1
    Possible duplicate of How to list npm user-installed packages? Commented Feb 6, 2017 at 15:35

8 Answers 8

291

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

Sign up to request clarification or add additional context in comments.

2 Comments

I like specifying --l or --long as well, npm -g ls --depth=0 --long. This provides the module descriptions and github links.
if you like npm ls full examples check this out: stackoverflow.com/questions/17937960/…
110

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' } 

3 Comments

Also, how can you obtain the file path of the modules folder?
path would be node_modules/[module name]. I believe this should work on all platforms. Note that this way only 'local' modules tree is printed, and requre looks first at node_modules, then ../node_modules, ../../node_modules ( see nodejs.org/api/… ) and then from NODE_PATH env var
try npm ls --parseable for just list of paths
50

List all globally installed third party modules, output to the console:

 npm -g ls 

Comments

29

In any os:

npm -g list 

and that's it.

1 Comment

This command will take longer than npm -g ls --depth=0, because it will also look for module dependencies.
13

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.

1 Comment

Thanks for specifying that npm has an API accessible from applications. How do you pass arguments to the functions? I tried npm.commands.ls(["depth=0"], ... ) but it gives me error and npm.commands.ls(["prod"], ... ) gives me an empty array....
6

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)); 

2 Comments

That won't give you packages installed globally
The original question doesn't specify whether or not they want to include globally installed packages. This answer provides a helpful alternative to the other answers.
3
for package in `sudo npm -g ls --depth=0 --parseable`; do printf "${package##*/}\n"; done 

1 Comment

Yeah, all my Bash nowadays has $()
0

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 └─┬ [email protected] ├── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └── [email protected] ├─┬ [email protected] │ ├── [email protected] │ └─┬ [email protected] │ └── [email protected] └── [email protected] 
  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 
  1. 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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.