npx & Package Executables
Many packages ship commands: esbuild, rollup, tsc, semver. A package declares them in its bin field; the package manager links them into node_modules/.bin; npm run and npx find them there. Understanding that chain explains "command not found" errors and why you rarely need global installs.
The bin field
{
"name": "@learner/shout",
"version": "1.0.0",
"bin": { "shout": "./cli.js" }
}The target file must start with a shebang (#!/usr/bin/env node) so Unix knows to run it with Node; on Windows npm generates .cmd and .ps1 wrappers instead. A package with a single command can write "bin": "./cli.js", and the command takes the package's name (without the scope).
Publishing and running a command
mkdir -p shout && cd shout
cat > package.json <<'JSON'
{ "name": "@learner/shout", "version": "1.0.0", "bin": { "shout": "cli.js" } }
JSON
cat > cli.js <<'JS'
#!/usr/bin/env node
const words = process.argv.slice(2);
console.log(words.join(' ').toUpperCase() + '!');
JS
npm publish > /dev/null 2>&1
mkdir ../app && cd ../app && npm init -y > /dev/null
npm install @learner/shout > /dev/null 2>&1
ls -l node_modules/.bin | sed 's/^.* shout/shout/'
npx shout hello there
npm exec -- shout from npm exec
npm pkg set scripts.hi="shout via a script" && npm run -s hi
command -v shout || echo "(shout is not on PATH outside npm)"total 0
shout -> ../@learner/shout/cli.js
HELLO THERE!
FROM NPM EXEC!
VIA A SCRIPT!
(shout is not on PATH outside npm)
node_modules/.bin/shout is a symlink to the package's cli.js, and npm made the file executable. It's only on PATH inside npm run, npx and npm exec; in your normal shell it isn't, which is a feature: each project uses its own version of each tool.
npx without a local install
When the command isn't installed locally, npx fetches the package into a cache (~/.npm/_npx), runs it, and keeps it there for next time. It asks first, unless you pass --yes (or run non-interactively, as in CI, where it fails instead of asking):
mkdir -p empty && cd empty
npx --yes semver@6.3.1 -r '^6' 6.3.1 7.8.5
npx --yes -p semver@7.8.5 -- semver -i major 7.8.5
ls ~/.npm/_npx | wc -l6.3.1
8.0.0
2
-p (--package) names the package when its command has a different name, or when you want a specific version. Put -- before the command so that its options (-i, --version…) aren't taken as npx's own. Each distinct package set gets its own folder in ~/.npm/_npx.
Tip: Pin the version in docs and CI (
npx --yes semver@7.8.5 …). A barenpx some-toolruns whatever version is latest today, which is both a reproducibility and a security problem.
Global installs
npm install -g puts a package in a global prefix and links its commands into <prefix>/bin. It's fine for tools you use everywhere (a package manager itself, a scaffolder), but a project should never depend on a globally installed tool, because a teammate's global version differs from yours.
npm config get prefix
npm install -g --prefix ~/tools --registry http://localhost:4873/ semver@7.8.5 2>&1 | grep added
ls ~/tools/bin
~/tools/bin/semver -i minor 1.2.3/opt/node
added 1 package in 481ms
semver
1.3.0
Here --prefix points at a folder in the home directory, the usual way to install global tools without sudo. The --registry flag is there for a reason worth knowing: the container's registry setting lives in the global npmrc, <prefix>/etc/npmrc (here /opt/node/etc/npmrc), and changing the prefix also changes which global npmrc npm reads. Without the flag, npm would ask registry.npmjs.org and fail offline. The .npmrc lesson covers the config layers. Add ~/tools/bin to your PATH (or set prefix=~/tools in ~/.npmrc) to use them by name.
| Want | Use |
|---|---|
| a project's build/test tool | devDependencies + npm run / npx |
| a one-off command | npx --yes pkg@version |
| a personal tool everywhere | npm install -g (into a user prefix), or pnpm dlx / yarn dlx for one-offs |
Test yourself
What does npm create for a dependency's bin entry?
- A copy in /usr/bin
- A link in node_modules/.bin
- An alias in package.json
- Nothing; you must run node path/to/cli.js
Why should CI scripts pin versions in npx commands?
- npx fails without a version
- A bare npx runs today's latest release, which can change behaviour or be compromised
- Pinned versions run faster
- npx ignores the cache otherwise
Which line must a JavaScript bin file start with on Unix?
- 'use strict'
- #!/usr/bin/env node
- module.exports =
- // @bin