Skip to content

Instantly share code, notes, and snippets.

@coryhouse
Last active October 26, 2023 21:41
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save coryhouse/b26f49bead69066844d9 to your computer and use it in GitHub Desktop.
Save coryhouse/b26f49bead69066844d9 to your computer and use it in GitHub Desktop.
Example of calling one script from another
{
"name": "npm-scripts-example",
"version": "1.0.0",
"description": "npm scripts example",
"scripts": {
"clean": "rimraf ./dist && mkdir dist",
"prebuild": "npm run clean",
"build": "cross-env NODE_ENV=production webpack"
}
}
@strobox
Copy link

strobox commented Jul 25, 2019

But if i use yarn?

@coryhouse
Copy link
Author

Then read the Yarn docs to learn the differences.

@camacho
Copy link

camacho commented Sep 24, 2019

@strobox - you could use yarn-or-npm in leu of npm to support using either the yarn or npm:

{
  "name": "npm-scripts-example",
  "version": "1.0.0",
  "description": "npm scripts example",
  "scripts": {
    "clean": "rimraf ./dist && mkdir dist",
    "prebuild": "yon run clean",
    "build": "cross-env NODE_ENV=production webpack"
  },
  "devDependencies": {
    "yarn-or-npm": "^3.0.1"
  }
}

@ashutosh-sharma
Copy link

How can we call two scripts in one script?
I want to call build-dev and build prod in one script say build.

 "scripts": {
    "start": "node src/server/index.js",
    "build-prod": "webpack --config webpack.prod.js",
    "build-dev": "webpack-dev-server  --config webpack.dev.js --open"
  },

@cwilby
Copy link

cwilby commented Oct 22, 2019

@ashutosh-sharma You can use && to run two or more commands in sequence.

 "scripts": {
    "start": "node src/server/index.js",
    "build": "npm run build-dev && npm run build-prod",
    "build-prod": "webpack --config webpack.prod.js",
    "build-dev": "webpack-dev-server  --config webpack.dev.js --open"
  }

@hpgmiskin
Copy link

@ashutosh-sharma and @cwilby you can also use npm-run-all

It provides a cross platform alternative to using & or &&

Run in series

npm run build-dev && npm run build-prod
npm-run-all build-dev build-prod

Run in parallel

npm run build-dev & npm run build-prod
npm-run-all --parallel build-dev build-prod

@emmiep
Copy link

emmiep commented Apr 28, 2021

But if i use yarn?

@strobox That's why I use $npm_execpath rather than npm or jest, whatever binary you're invoked will be run then.

"prebuild": "$npm_execpath run clean"

@J-Cake
Copy link

J-Cake commented Jan 14, 2022

@strobox That's why I use $npm_execpath rather than npm or jest, whatever binary you're invoked will be run then.

Dude that's genius

@christopher-hopper
Copy link

@strobox That's why I use $npm_execpath rather than npm or jest, whatever binary you're invoked will be run then.

OMG thank you sir. So good.

@x11x
Copy link

x11x commented Jul 6, 2022

Note that $npm_execpath is not cross-platform (Windows requires %npm_execpath%).

Another option that supports npm, pnpm, yarn is yarpm.

Beware when using any of these options that package manager might have subtly different behaviors when running scripts, make sure you know what your scripts do in all package managers if you want to make it package manager independent like this. Common cases like the above should be fine, but i think yarn does different things with "script:foo" type scripts.

dum is also an interesting take - a standalone package.json scripts runner, independent of the package manager.

See also a discussion about whether this functionality should be added to node itself (through the new experimental corepack feature) and associated problems/challenges. I also learned on that issue that yarn supports running another script through just a "run" command, so example above would be "run build-dev && run build-prod". Strange I can't find that anywhere in yarn's docs, it does not a seem well-advertised feature.

@J-Cake
Copy link

J-Cake commented Jul 7, 2022

Note that $npm_execpath is not cross-platform (Windows requires %npm_execpath%).

Just force everyone to use unix... ¯\_(ツ)_/¯

@x11x
Copy link

x11x commented Jul 7, 2022

If you are happy to force everyone to use a specific setup, its probably easier to force everyone to use a specific package manager: npm (it is already installed), or your package manager of choice (it is only a npm -g i away).

I think the best option is: if you are the primary developer/maintainer, just hard-code your package manager of choice rather than using $npm_execpath. And set the packageManager field in package.json to support usage with corepack, which is now included in Node.

If your team/organization really can't decide on a package manager, I think its worth using yarpm/ni/dum/npm-run-all rather than forcing everyone to use a Posix shell. Most npm packages are targeting NodeJS or the browser, which are cross-platform environments, so it does not make much sense to rely on OS-specific features.

@x11x
Copy link

x11x commented Jul 7, 2022

I should note if you are using corepack, you should use corepack enable, not npm -g install .... See the docs

@gulhe
Copy link

gulhe commented Mar 15, 2023

If you are happy to force everyone to use a specific setup

Joke aside: it's not bad practice to write your package.json (or any source for that matter) as it'll be used by your CI and stop there.

Anyone wants anything fancier/newer/better/more-comfy they may do it on their machine but it's up to them.

Joke back on: not my fault if (close to) all CIs are on unix ¯\_(ツ)_/¯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment