简体   繁体   中英

How to install all existing global NPM packages into local directory?

This is not similar nor does it answer question: copy npm global installed package to local

Basically I want to tell NPM, look at what I have installed globally, and install that locally.

I'm not interested in the symlinks, but if above is not possible, could I use a single symlink to node_modules instead of a symlink for each package?

You can parse the output of node ls -g --depth 0 and npm install the resulting list of packages.

npm i -S -dd $(npm ls -g --depth 0 | tail -n +2 | cut -c 5- | tr '\n' ' ')

Run this command in the directory of the package that you wish to install global packages to.

Explanation:

npm i -S -dd Shorthand for npm install --save --verbose . The -S is unnecessary on recent npm versions that save installed packages to package.json by default.

npm ls -g --depth 0 List first-level global packages.

tail -n +2 Remove the first line from the output.

cut -c 5- Remove the first four characters from each line in the output.

tr '\\n' ' ' Combine each line to place all packages on one line, separated by spaces.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM