Create a custom npm script

Elijah Manor
InstructorElijah Manor

Share this video with your friends

Send Tweet
Published 5 years ago
Updated 3 years ago

A lot of the power behind npm scripts is creating custom scripts that are outside the basic set that npm natively understands. In this lesson we will install the ESLint node package, create a new eslint npm script, briefly discuss about environment variables and how npm knows where to find your binary, and then execute our custom script from the command line.

Instructor: [00:01] Let's create a new custom npm script called eslint. First, we'll come down to our tests and make a new entry for eslint. Here, we'll call eslint.

[00:11] We'll pass the catch flag so it won't re-lint things that haven't changed. We'll pass the fix flag, which will auto-fix certain rules that have simple solutions. Then we'll tell eslint to lint all the files in the current path.

[00:23] Many developers don't like to have many globally installed node modules. You might be tempted to reference the locally installed version of eslint by referencing the node_modules bin folder, or you might be inclined to use the npm bin command.

[00:40] However, npm does provide many environment variables for you. If you type "npm run environment," it'll list all the environment variables that it adds or updates.

[00:50] If we run that again and search for the path, then we'll see that npm adds our local bin folder for us, which means we can remove all the additional path information. We'll use the local version.

[01:06] Now we can go to our terminal and type "npm run-script eslint," or, for short, we could just say, "npm run eslint." That will run our new custom npm script.

xiao cliton
xiao cliton
~ 2 years ago

Hi, Elijah!

What is your opinion about gulp? Do you think webpack + npm script will kill gulp in the future?