简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM