Using npm as a build tool
FreeReplace Grunt and Gulp with npm scripts for simpler builds
FreeFree tier
About Using npm as a build tool
A comprehensive guide by Keith Cirkel explaining how to use npm scripts as an alternative to build tools like Grunt and Gulp. The post details how npm's scripts directive in package.json can manage build tasks more succinctly, elegantly, and with fewer dependencies. It covers using npm run to execute shell commands, automatic inclusion of node_modules/.bin in the PATH, and shortcut commands like npm test and npm start. This resource is intended for developers looking to simplify their build pipeline by leveraging npm's built-in capabilities.
Key Features
Use npm run scripts defined in package.json for build tasks
Automatic PATH includes node_modules/.bin for installed binaries
Shortcut commands: npm test, npm start, npm stop
No extra build tool dependencies needed
Runs commands in the operating system's default shell (usually Bash)
Pros & Cons
Pros
- More succinct and elegant than Grunt or Gulp
- Fewer package dependencies and less maintenance overhead
- Uses familiar npm commands and configuration
- No need for a separate build tool configuration file
- Leverages existing shell skills
Cons
- Limited to shell commands; complex workflows may require additional tools like npm-run-all
- No built-in parallel or concurrent task execution (can be achieved with external scripts)
- Requires familiarity with command-line scripting on the target OS
- Lacks the rich plugin ecosystem of dedicated build tools
Best For
Replacing Grunt, Gulp, or other task runnersRunning linters (e.g., JSHint) via npm scriptsRunning test suites (e.g., Mocha) through npm testBrowserifying modules using npm scriptsSimplifying build pipelines for Node.js or frontend projects
FAQ
How do I run a script defined in package.json?
Use `npm run <script-name>`. For example, if you have a script named 'lint', run `npm run lint`.
Can npm scripts access locally installed binaries without specifying the full path?
Yes, npm adds `node_modules/.bin` to the PATH when running scripts, so binaries from dependencies can be called directly.
What are the shortcut commands for npm scripts?
`npm test`, `npm start`, and `npm stop` are shortcuts that run the corresponding scripts in package.json without needing `npm run`.
Is this method still recommended today?
Yes, the author states the advice still stands and that using npm as a build tool is recommended, with the note that he has been Gulp & Grunt free since 2013.