Skip to content

Instantly share code, notes, and snippets.

@conartist6
Last active May 25, 2022 17:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save conartist6/4f784765b76977ab3b95dc2b26ee9a15 to your computer and use it in GitHub Desktop.
Save conartist6/4f784765b76977ab3b95dc2b26ee9a15 to your computer and use it in GitHub Desktop.
Node package scripts

Node packages usually ship with scripts that are useful to developers. Common scripts might be build, test, or lint, but you will need to look inside the package.json file to see what scripts are actually available for a particular package. Here is what a common package with a build script might look like:

{
  "name": "my-package",
  "version": "0.1.0",
  "scripts": {
    "build": "transpile -f lib/index.ts -o /lib/index.js"
  },
  "devDependencies": {
    "transpile": "0.99.01"
  }
}

Setup to run a script

To run a js package script you need the following things:

  • nodejs
  • A package manager
    • The npm client comes with nodejs, but other common clients are yarn and pnpm
  • The package's dependencies must be installed (including devDependencies)

Commands by package manager

Once you are ready, open your favorite terminal and enter the command assicated with your package manager:

npm run script-name
pnpm run script-name
yarn run script-name
# or yarn even supports
yarn script-name

If you're not sure which of those to pick, read on!

Passing flags

The safest way to pass flags to your script is to use --, which all package managers interpret as meaning that the remaining arguments are for the script being run. For example:

npm run lint -- --quiet

It is also often fine to omit the extra --, but this will be dependent on which manager you use and what version.

Which package manager should I use?

You must use the package manager you used to install dependencies to run scripts. This is because packages can define certain scripts as named "binaries" which will be in the path for the script (meaning they can be called by name).

As to which package manager to choose for installing, usually the correct choice is the manager used by the individual who wrote the code, at least if they have incorporated a lockfile into their project.

manager indicative files:
npm package-lock.json
pnpm pnpm-lock.yaml
yarn yarn.lock, .pnp.cjs

If you do not have the correct manager and running the script is all you need, you can always try installing and running a script with the manager you have (likely npm). Juse be aware that it might not work.

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