6

I need to use the version attribute of my package.json in one of it's scripts to put the version in the name of the JS bundle (i prefer to use the version as a unique identifier instead of a hash).

what i have:

"build-js": "browserify -t [ babelify --presets [ react es2015 ] ] js/components/App.js -o js/bundle.js"

what i need (i know it doesn't parse, but you get the idea):

"build-js": "browserify -t [ babelify --presets [ react es2015 ] ] js/components/App.js -o js/bundle."+this.version+".js"
1
  • so what is the question? Commented Mar 13, 2017 at 15:51

2 Answers 2

9

When you run an npm script, npm will set all the package.json fields as environment variables that you can use:

https://docs.npmjs.com/cli/v9/using-npm/scripts#packagejson-vars

you can use the npm_package_version environment variable

Sign up to request clarification or add additional context in comments.

Comments

5

You can do this with:

"build-js": "browserify -t [ babelify --presets [ react es2015 ] ] js/components/App.js -o js/bundle.$npm_package_version.js"

See this video for more details: https://egghead.io/lessons/tools-use-custom-config-settings-in-your-npm-scripts

P.S. It's worth not putting your bundle in the same directory as your source, usually it goes into dist/, e.g. dist/bundle.js. This way if you are publishing to npm you can ignore the unbuilt source directory. Or you can just remove the dist/ dir and rebuild, plus in the future you'll want other assets in dist as part of your build process.

1 Comment

Thank you, i'll follow your tip

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.