What’s the npm scripts

coding.jpg Thousands of developers create amazing things using JavaScript.
But even the most fascinating projects include a lot of repetitive tasks.
Every coder would enjoy saving time on the routine and dedicate it to solving thought-provoking tasks. JS npm scripts are the Cinderella of the coding world, in other words, they do the routine processes for you automatically.
And that is not the only advantage of npm scripts.

There are a lot of different tools to make the job of a software developer easier.
Even if you haven’t used them yourself by now, you’ve probably heard about some of them.
Grunt, Brunch, Gulp, Broccoli are some of the most popular ones.
They surely make running routine tasks much easier, especially if you work on big projects.
But there may be some negative side effects to using them.
All of those tools have their own quirks, bugs, syntax, and requirements that you should keep in mind. There is also an issue with the plugin dependencies and version compatibility. It’s cool if you never had such problems with the tools you use, but sometimes you might face one of those problems. If you begin to feel that instead of concentrating on coding you are fighting with your tools then you might think about npm scripts.

While running task runners you would expand the package.json.
The scripts would be going to the object called scripts and the dependencies from the installed tools would be going to the devDependencies. The devDependencies object is filled simply by installing the tools you’ll use by npm install. The dependencies are very useful not only for you but to the other people who might have to install your project on their computer, as it will help them to install all the necessary tools to work with your code. As for the scripts, you can put any valid command-line commands there. You can minify JavaScript, compile SCSS to CSS, compress the images and other useful but boring tasks. Add something like “scriptname” : “script code itself“ to the scripts object of package.json. Then you can run that script by calling npm run scriptname from the command line. You can add any number of scripts just like that. To make the routines even more simple, you can combine the tasks into bundles by writing something like “tasks”: “npm run script1 && npm run script2”.
It gives you a flexible and quick instrument to order and run routine tasks.
I hope it was easy, and you now know how to keep your npm scripts in one place and configure them easily so you can focus more on writing code.



Boostlog is an online community for developers
who want to share ideas and grow each other.

Delete an article

Deleted articles are gone forever. Are you sure?