Print a list of all installed node.js modules

后端 未结 7 2042
情话喂你
情话喂你 2020-12-22 17:29

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(__fil         


        
7条回答
  •  旧巷少年郎
    2020-12-22 17:44

    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.

提交回复
热议问题