工作中在老项目中常常由于项目过大或依赖严重等历史原因,导致无法(不敢)升级某些库,但是又想在后续的开发中使用新版本的特性,此时可能需要同时装多个版本来满足需求,但是执行同样的一行 npm install xxx@version
是只能同时装一个版本的,那么应该怎么做呢?
安装不同版本的库
官方也想到了此场景,给出的解决方案是:
npm install <alias> @npm:<name>
示例:
npm install my-react@npm:react
npm install jquery2@npm:jquery@2
npm install jquery3@npm:jquery@3
npm install npa@npm:npm-package-arg
安装对应版本的@types
也是别名的方式:
npm install <name>@npm:@types/<name>
示例:
npm install @types/lodash4@npm:@types/lodash
参考:https://docs.npmjs.com/cli/v6/commands/npm-install
查看包的所有版本
npm view <name> versions
示例:
npm view lodash versions