Scripts in NPM are aliases for terminal script sequences, which we can define to run them comfortably ourselves, or the future users of our project.
For example, we can create a script for automating complicated tasks, such as running tests, starting the development server, building the application, among others.
The use of scripts is very common. In fact, it is most normal for you to have several scripts in any project. In this way, instead of remembering and executing individual commands, it is as convenient as using the script.
Also, it has the important advantage that you make sure that all team members use the same commands. Or, if it is an Open Source code, they know how to execute the defined actions.
How to use NPM scripts
Scripts are defined in the package.json
file of our project. You can create and customize your own NPM Scripts according to the needs of your project.
Creating a script
To create a custom script in NPM, we must define it in the "scripts"
section of our package.json
file.
Each script has a unique name and is defined using the format "name": "command"
. For example:
"scripts": {
"launch_notepad": "notepad"
}
With this script on Windows, we would launch Notepad. It’s not particularly useful, but it’s enough for the example 😃.
Running an NPM Script
Then, simply write the npm run
command followed by the name of the script you want to run:
npm run launch_notepad
In the case of our simple command example, the name would be launch_notepad
Passing arguments to custom scripts
In some cases, it may be necessary to pass arguments to our custom scripts. It’s not too common, but it’s possible.
You can pass parameters to your NPM Scripts by adding two dashes followed by the arguments after the script name.
npm run notepad -- my_text_file.txt
Be careful, you need to put a --
after the name of the script you want to run. This tells npm
that what comes next are the parameters of the Script.
In our example, the Script would launch Notepad and pass my_text_file.txt
as a parameter. So, Notepad would open with this file open. Which again, is not particularly useful, but it serves as an example for me.
More realistic examples could be, for example, running the setup
script with a configuration argument as follows:
npm run setup -- --config=production
Pre and Post Scripts
NPM offers the possibility of running scripts before and after a specific script. Use the following names for pre and post scripts:
"scripts": {
"prestart": "echo 'Before starting'",
"start": "echo 'Start'",
"poststart": "echo 'After start'",
}
When running the npm run start
command, the prestart
, start
, and poststart
scripts will be automatically executed in that order.
Running scripts in sequence
Sometimes, you may need to run multiple scripts in sequence, one after the other. To achieve this, you can use the &&
operator in the command line. For example:
npm run build && npm run test
In the previous command, the script1
will be executed and, if it ends correctly, the script2
will be executed.
This is useful, for example, if we want to create commands that launch other tasks in order.
"scripts": {
"launch": "npm run check && npm run build",
"check": "echo 'Check'",
"build": "echo 'Build'",
}
In this case, the launch
Script would execute the check
Script and then the Build
Script. In the example, these two scripts only display text on the console. But in a real project, they would have our logic.