简体   繁体   中英

How to install only babel related devDependencies in package.json?

the devDependencies section in my project looks like this:

  "devDependencies": {
    "@babel/cli": "^7.1.5",
    "@babel/core": "^7.1.6",
    "@babel/node": "^7.0.0",
    "@babel/preset-env": "^7.1.6",
    "@babel/register": "^7.0.0",
    "chai": "^4.2.0",
    "chai-http": "^4.2.0",
    "eslint": "^5.8.0",
    "eslint-config-airbnb-base": "^13.1.0",
    "eslint-plugin-import": "^2.14.0",
    "mocha": "^5.2.0",
    "nodemon": "^1.18.5"
  },

they are all dependencies for development.

But @babel/cli , @babel/core , @babel/preset-node are the only dev dependencies that my build machine needs to compile the ES6/ES7 source to dist which is for production.

My building machine is a jenkins slave, which run the building jobs in one time only, with --rm option, docker containers. To optimize the DevOps process, in this compiling job, all other deps, even the production deps are not necessary.

But according to the npm/install doc , the best command for me is

npm install --only=development

that I think is still over killing.

If I run npm install @babel/cli @babel/node @babel/preset-env , they would be added to the "dependencies" section in package.json.

If I run npm install @babel/cli @babel/node @babel/preset-env -D , they would overwrite to the "devDependencies" section in package.json.

In these two approaches, npm would not honor the version tag in package.json or package-lock.json.

I have also considered that putting all other devDeps, like chai, mocha, to optionalDependencies , but it is not what this section intended to mean, and there is no command to install optional only deps with npm install .

OK. To be clear. Your problem is after NPM 5.8.0. When they change the --save option on by default.

For your problem. I think you can write a bash function something like:

function npm-temp-install {
    pkg=`cat package.json | jq -r ".devDependencies.$1" | sed "s/^/$1@/"`
    npm install $pkg -D
}

Then you can run npm-temp-install @babel/cli to install the package with the specific version that listed in your devDependencies .

For your first comment: I think you can use npx to run nodemon. From its website . It said it will help you download the package if the command is not found in node_modules/.bin . And it won't change the package.json or package-lock.json .

删除 package-lock.json 并运行

npm install @babel/cli @babel/node @babel/preset-env -D

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