Fix all node modules' version numbers to the currently using ones in package.json

こ雲淡風輕ζ 提交于 2020-01-23 14:43:05

问题


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?

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


回答1:


You're looking for

npm shrinkwrap

See the documentation here for more information.

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.

UPDATE

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;
}

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



来源:https://stackoverflow.com/questions/33323296/fix-all-node-modules-version-numbers-to-the-currently-using-ones-in-package-jso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!