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"
}
}
To run a js package script you need the following things:
- nodejs
- A package manager
- The package's dependencies must be installed (including
devDependencies
)
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!
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.
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.