

We can explicitly define which loglevel we want to use when running a command, using -loglevel. Any logs of a higher level than the currently defined will be shown. The log level determines which logs will be displayed in the output. There are different log levels: "silent", "error", "warn", "notice", "http", "timing", "info", "verbose", "silly". We saw how we can reduce logs using -silent, but what about getting even more detailed logs? Or something in between? ℹ️ If we don't want to get an error when the script doesn't exists, we can use -if-present instead: npm run -if-present. Maybe in a CI pipeline, you want your whole pipeline to keep running even when the test command fails. The -silent flag (short for -loglevel silent) can be helpful when you want to run a script that you know may fail, but you don't want it to throw an error. Use npm run -silent to reduce logs and to prevent the script from throwing an error.

On failure, all logs are included in this file. The path to this file is provided at the end of the error message. Both can be useful for googling the error.Īnd if we need more information, we can always access the complete log file. When a script throws an error, we get a few other details, such as the error number errno and the code. To define an NPM script, all you need to do is set its name and write the script in the script property in your package.json file:Įnter fullscreen mode Exit fullscreen mode

You can also use these scripts in your CI/CD pipeline to simplify tasks like build and generate test reports. Their goal is to provide a simple way to execute repetitive tasks, like: NPM Scripts are a set of built-in and custom scripts defined in the package.json file.
#Npm config set main how to#
In this article, I'll share my research about how to take full advantage of NPM scripts. That's when I decided to learn everything I could about NPM scripts and write this article. I've been using NPM Scripts for years, but I wanted to pass a parameter to a script a few weeks ago and realized I didn't know how to do that. But do you know all you can do with NPM Scripts? You may have come across the scripts property in the package.json file and even write some scripts yourself.
