152

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?
Federico Baù
  • 6,013
  • 5
  • 30
  • 38
Anderson Green
  • 30,230
  • 67
  • 195
  • 328
  • 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. – Anderson Green Dec 20 '12 at 23:05
  • Information about getting the list of files from a directory: http://stackoverflow.com/questions/2727167/getting-all-filenames-in-a-directory-with-node-js – Anderson Green Dec 20 '12 at 23:07
  • 1
    Possible duplicate of [How to list npm user-installed packages?](http://stackoverflow.com/questions/17937960/how-to-list-npm-user-installed-packages) – Mehdi Dehghani Feb 06 '17 at 15:35

8 Answers8

285

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

seenukarthi
  • 8,241
  • 10
  • 47
  • 68
aniston
  • 3,007
  • 2
  • 11
  • 3
  • 7
    I like specifying --l or --long as well, `npm -g ls --depth=0 --long`. This provides the module descriptions and github links. – Adam Caviness May 02 '15 at 00:28
  • if you like `npm ls` full examples check this out: http://stackoverflow.com/questions/17937960/how-to-list-npm-user-installed-packages/40784174#40784174 – prosti Dec 03 '16 at 01:50
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' }
Andrey Sidorov
  • 24,905
  • 4
  • 62
  • 75
  • Also, how can you obtain the file path of the modules folder? – Anderson Green Dec 21 '12 at 00:21
  • 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 http://nodejs.org/api/modules.html#modules_loading_from_node_modules_folders ) and then from NODE_PATH env var – Andrey Sidorov Dec 21 '12 at 01:28
  • 4
    try `npm ls --parseable` for just list of paths – Andrey Sidorov Dec 21 '12 at 01:33
50

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

 npm -g ls
hfarazm
  • 1,407
  • 17
  • 22
29

in any os

npm -g list

and thats it

12

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.

d4nyll
  • 11,811
  • 6
  • 54
  • 68
  • 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.... – Giuliano Collacchioni Jul 01 '20 at 17:33
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));
neojp
  • 457
  • 5
  • 10
3
for package in `sudo npm -g ls --depth=0 --parseable`; do
    printf "${package##*/}\n";
done
A T
  • 13,008
  • 21
  • 97
  • 158
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
     └─┬ 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 cowsay@1.2.0

    Global package

    npm install -g webpack@4.16.4

  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

Federico Baù
  • 6,013
  • 5
  • 30
  • 38