Lijin Durairaj
Lijin Durairaj

Reputation: 3511

How to call one command from another in package.json?

suppose if i have some script in package.json like this

 "scripts": {
    "a1": "first command",
    "a2": "second command",
    "a3": "third command",
      
  }

and if i want to run script a1 and a2 in the script a3 how can i do this? can this be possible? I am using

node version : v6.9.4

npm version : 4.3.0

i want to achieve something like this

"scripts": {
    "a1": "first command",
    "a2": "second command",
    "a3": "third command && a1 && a2",
      
  }

Upvotes: 29

Views: 14200

Answers (2)

Awais Niaz
Awais Niaz

Reputation: 49

you can run one script from to another script is just mention a target command into source command script.

"scripts": {
"dev": "npm run format && npm run lint &&  vite",
"build": "npm run format && npm run lint && tsc && vite build",
"lint": "eslint . --fix --max-warnings=0",
"format": "prettier . --write",
"preview": "vite preview",
"lint-staged": "lint-staged --config lint-staged.js",
"husky-install": "husky install"

}

Upvotes: -1

RobC
RobC

Reputation: 25042

Use npm run inside the script. E.g.

"scripts": {
  "a1": "first command",
  "a2": "second command",
  "a3": "third command && npm run a1 && npm run a2",
}

Running $ npm run a3 via the CLI will run the third command (whatever that is), followed by a1, then a2.

However, if running $npm run a3 via the CLI is to only run a1 followed by a2 then:

"scripts": {
  "a1": "first command",
  "a2": "second command",
  "a3": "npm run a1 && npm run a2",
}

Upvotes: 47

Related Questions