npm package.json config variables used for version numbers

1.4k Views Asked by At

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?

1

There are 1 best solutions below

0
On BEST ANSWER

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