简体   繁体   English

如何将所有现有的全局 NPM 包安装到本地目录中?

[英]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这既不相似也不回答问题: copy npm global installed package to local

Basically I want to tell NPM, look at what I have installed globally, and install that locally.基本上我想告诉 NPM,看看我在全局安装了什么,然后在本地安装。

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?我对符号链接不感兴趣,但如果以上不可能,我可以使用单个符号链接到 node_modules 而不是每个包的符号链接吗?

You can parse the output of node ls -g --depth 0 and npm install the resulting list of packages.您可以解析node ls -g --depth 0的输出并npm install生成的包列表。

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 . npm i -S -dd npm install --save --verbose简写。 The -S is unnecessary on recent npm versions that save installed packages to package.json by default.在默认情况下将安装的包保存到 package.json 的最新 npm 版本中-S是不必要的。

npm ls -g --depth 0 List first-level global packages. npm ls -g --depth 0列出一级全局包。

tail -n +2 Remove the first line from the output. tail -n +2从输出中删除第一行。

cut -c 5- Remove the first four characters from each line in the output. cut -c 5-从输出中的每一行中删除前四个字符。

tr '\\n' ' ' Combine each line to place all packages on one line, separated by spaces. tr '\\n' ' '组合每一行,将所有包放在一行,用空格分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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