将所有节点模块的版本号固定为package.json中当前使用的版本号 [英] Fix all node modules' version numbers to the currently using ones in package.json

查看:552
本文介绍了将所有节点模块的版本号固定为package.json中当前使用的版本号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当前,package.json中的所有节点模块都使用*作为版本号,我的应用程序对那些节点模块非常稳定,因此我想在package.json中修复它们的版本号,以便我可以运行npm install在其他位置以安装具有预期版本的节点模块.

Currently, all node modules in package.json are using * as the version number, my application is quite stable with those node modules, so I want to fix their version numbers in package.json, so that I can run npm install in other places to install node modules with expected versions.

有没有一种方法可以快速完成,而不是手动逐个更改?

Is there a way to do it quickly instead of changing them one by one manually?

例如某些控制台命令,npm fixversion module_a module_b ...?

Such as some console commands, npm fixversion module_a module_b ...?

推荐答案

您正在寻找

npm shrinkwrap

有关更多信息,请参见此处的文档.

See the documentation here for more information.

它将生成具有当前版本的npm-shrinkwrap.json,并且优先于package.json,因此您可以删除该文件,并根据需要删除npm update.

It will generate an npm-shrinkwrap.json with the current versions, and it takes precedence over package.json, so you can delete that file and npm update if you wish.

更新

这是一个小脚本,它将具有npm-shrinkwrap.json版本的package.json写入新文件package-lockdown.json:

Here is a little script that writes out the package.json with the versions from the npm-shrinkwrap.json to a new file, package-lockdown.json:

var fs = require('fs');
var p = JSON.parse( fs.readFileSync( 'package.json') );
var v = JSON.parse( fs.readFileSync( 'npm-shrinkwrap.json') );

updateDependencies( p.dependencies,    v.dependencies );
updateDependencies( p.devDependencies, v.dependencies );

fs.writeFileSync( 'package-lockdown.json', JSON.stringify( p, null, 2 ) );

function updateDependencies( list, v )
{
        for ( var d in list )
                list[d] = v[d].version;
}

上面的脚本也会同时更新devDependencies,因此请确保在运行脚本之前删除该行或运行npm shrinkwrap --dev.

The above script updates devDependencies aswell, so be sure to either remove that line or run npm shrinkwrap --dev before running the script.

这篇关于将所有节点模块的版本号固定为package.json中当前使用的版本号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆