{"id":237960,"date":"2016-02-12T09:12:45","date_gmt":"2016-02-12T16:12:45","guid":{"rendered":"http:\/\/css-tricks.com\/?p=237960"},"modified":"2020-08-18T19:49:41","modified_gmt":"2020-08-19T02:49:41","slug":"why-npm-scripts","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/why-npm-scripts\/","title":{"rendered":"Why npm Scripts?"},"content":{"rendered":"\n
There has been a growing sentiment (for instance<\/a>) that using node packages directly, with the command line interfaces they provide, is a good route to take. As opposed to abstracting the functionality away behind a task runner. Partly: you use npm anyway, npm provides scripting functionality, why not just use that? But there is more to it than that. Let’s walk through the thinking, but also exactly how to accomplish many of the most important tasks in a front end development build process.<\/p>\n\n\n\n\n\n\n\n I\u2019ve started using npm scripts<\/a> in my projects for about the last six months. Before that, I used Gulp<\/a> and before that, Grunt<\/a>. They’ve served me well and helped me perform my work faster and more efficiently by automating many of the things I used to do by hand. However, I started to feel that I was fighting the tools rather than focusing on my own code.<\/p>\n\n\n\n Grunt, Gulp, Broccoli, Brunch and the like all require you to fit your tasks into their paradigms and configurations. Each has it\u2019s own syntaxes, quirks and gotchas that you need to learn. This adds code complexity, build complexity and makes you focus on fixing tooling rather than writing code.<\/p>\n\n\n\n These build tools rely on plugins that wrap a core command line tool. This creates another layer of abstraction away from the core tool, which means more potential for bad things to happen.<\/p>\n\n\n Let me say this: if you are happy with your current build system and it accomplishes all that you need it to do, you can keep using it!<\/strong> Just because npm scripts are becoming more popular doesn\u2019t mean you should jump ship. Keep focusing on writing your code instead of learning more tooling. If you start to get the feeling that you\u2019re fighting with your tools, that’s when I’d suggest considering npm scripts.<\/p>\n\n\n\n If you\u2019ve decided you want to investigate or start using npm scripts, keep reading! You\u2019ll find plenty of example tasks in the rest of this post. Also, I\u2019ve created npm-build-boilerplate<\/a> with all of these tasks that you can use as a starting point. Let\u2019s get to it!<\/p>\n\n\n We\u2019ll be spending a majority of our time in a `package.json` file. This is where all of our dependencies and scripts will live. Here\u2019s a stripped down version from my boilerplate project:<\/p>\n\n\n\n We\u2019ll build up our `package.json` file as we go along. Our scripts will go into the Before we begin, here’s a sample structure of the project I’ll be referring to throughout this post:<\/p>\n\n\n\nHere are three problems I\u2019ve seen multiple times:<\/h3>\n\n\n
But, bear in mind…<\/h3>\n\n\n
Writing npm scripts<\/h3>\n\n\n
{\n \"name\": \"npm-build-boilerplate\",\n \"version\": \"1.0.0\",\n \"scripts\": {\n ...\n },\n \"devDependencies\": {\n ...\n }\n}<\/code><\/pre>\n\n\n\n
scripts<\/code> object, and any tools we want to use will be installed and put into the
devDependencies<\/code> object.<\/p>\n\n\n\n