Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get version level from npm script

Tags:

node.js

npm

I want to write npm scripts which run when bumping the package version via npm version. I do not intend for my scripts to be invoked directly via npm run _____; instead, they should be invoked by npm, when npm version ____ (or preversion or postversion, etc) is called.

How can I refer to the version level argument in my scripts?

e.g. If my script runs as preversion and was invoked from npm version major, how can my script refer to major?

like image 403
tuff Avatar asked May 10 '26 22:05

tuff


1 Answers

Within my script, I can refer to process.env.npm_config_argv. Its value is a JSON string which contains the original args to npm.

If my package.json contains:

"scripts": {
  "preversion": "node log_argv"
}

And log_argv.js contains:

console.log('Type:', typeof process.env.npm_config_argv);
console.log('Value:', process.env.npm_config_argv);
console.log('Original npm args:', JSON.parse(process.env.npm_config_argv).original);

throw new Error("aborting");

And I run npm version patch, then I see the output:

Type: string
Value: {"remain":["patch"],"cooked":["version","patch"],"original":["version","patch"]}
Original npm args: [ 'version', 'patch' ]
like image 91
tuff Avatar answered May 12 '26 14:05

tuff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!