Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm package.json config variables used for version numbers

I have 3 React-related packages in my package.json, and the version numbers must be in sync. I know with Maven, you can define variables in the POM file to re-use and keep version numbers in sync across different packages.

I want to do the same thing with my npm package.json, like so:

...
"config": {
  "react_version": "^15.4.1"
},
"dependencies": {
  "react": "$npm_package_config_react_version",
  "react-addons-test-utils": "$npm_package_config_react_version",
  "react-dom": "$npm_package_config_react_version"
}
...

It seems that config variables set in the package.json file can only be used inside your script commands.

Is there a way to solve this problem at the moment? Will something like this be included in a future version of npm?

like image 206
Atif Avatar asked Dec 22 '16 22:12

Atif


People also ask

How do I specify node version in package json?

Use the engines keyword in the package. json file to specify the Node. js version that you want your application to use. You can also specify a version range using npm notation.


1 Answers

I've been wrestling with this too. The issue for me has been that I want to use different npm tags in different environments ('latest' for dev, 'prod' for prod). The solution I came up with is to use an environment variable for the tag. I set up something along the following in package.json. Since I'm using 'latest' everywhere except for production servers, I avoid the problem of inadvertently changing the git repo:

  "scripts": {
    "start": "perl -pi -e \"s#\\\"package_name\\\".*#\\\"package_name\\\": \\\"$TAG_VAR\\\",#\" package.json && node app.js"
  },
  "dependencies": {
    "package_name": "latest",
    "other_package": "^1.0.0"
  }
like image 119
Guy Rom Avatar answered Oct 01 '22 03:10

Guy Rom