DEV Community

Pavan C
Pavan C

Posted on

NPM Scripts You Didn’t Know You Could Write

After working with JavaScript, you probably know how to run your app and test it using npm scripts. But npm scripts are more than simple command aliases. They’re a surprisingly powerful automation layer baked right into your project.

If you haven’t explored beyond the basics, you’re missing out on ways to simplify complex workflows without adding bulky tooling.

Here are some npm scripting patterns and techniques that can seriously level up your developer experience.

1. Use npm scripts to orchestrate complex workflows

Instead of running multiple commands manually, chain and orchestrate them cleanly with npm scripts:

"scripts": {
  "lint": "eslint .",
  "test": "jest",
  "build": "webpack --mode production",
  "ci": "npm run lint && npm run test && npm run build"
}
Enter fullscreen mode Exit fullscreen mode

Now npm run ci runs your full pipeline in a predictable sequence.

2. Run scripts in parallel without external tools

Parallel execution is often useful (e.g., starting dev servers and watchers simultaneously). Instead of pulling in dependencies, use npm-run-all:

npm install --save-dev npm-run-all
Enter fullscreen mode Exit fullscreen mode

Then in your scripts:

"scripts": {
  "start:api": "node api.js",
  "start:web": "webpack-dev-server",
  "start": "npm-run-all --parallel start:api start:web"
}
Enter fullscreen mode Exit fullscreen mode

This keeps your workflow lightning fast.

3. Accept and forward CLI arguments dynamically

Pass arguments through npm scripts cleanly using --:

"scripts": {
  "build": "webpack"
}
Enter fullscreen mode Exit fullscreen mode

Run with:

npm run build -- --watch --env production
Enter fullscreen mode Exit fullscreen mode

webpack receives --watch --env production transparently, no changes needed in the script.

4. Define environment variables inline, cross-platform

Environment variables are tricky across Windows and Unix. Use cross-env for smooth cross-platform scripts:

npm install --save-dev cross-env
Enter fullscreen mode Exit fullscreen mode
"scripts": {
  "serve": "cross-env NODE_ENV=production node server.js"
}
Enter fullscreen mode Exit fullscreen mode

This avoids the headache of platform-specific env setting.

5. Leverage shell features in scripts

Npm scripts run in your shell, so you can:

Use globbing, conditionals, and loops:

"scripts": {
  "clean": "rm -rf dist/*.js",
  "prepublish": "if [ -d dist ]; then echo 'Ready to publish'; else echo 'Build missing'; fi"
}
Enter fullscreen mode Exit fullscreen mode

Windows users can use powershell or Node.js scripts for complex logic.

6. Create script aliases for complex or repetitive tasks

Simplify your commands and improve discoverability by adding aliases:

"scripts": {
  "ci": "npm run lint && npm run test && npm run build",
  "start-dev": "webpack serve --mode development --open"
}
Enter fullscreen mode Exit fullscreen mode

Aliases reduce cognitive overhead and speed up onboarding.

7. Debug node scripts via npm scripts

Pass flags to node directly inside npm scripts:

"scripts": {
  "debug": "node --inspect-brk index.js"
}
Enter fullscreen mode Exit fullscreen mode

Now run npm run debug and attach your debugger immediately.


What this really means

Npm scripts aren’t just trivial shortcuts — they’re a robust, flexible automation layer that can replace heavier tools for many tasks. Using them strategically saves time, reduces complexity, and improves team workflows.

Happy Coding!

Top comments (0)