This Lesson is for Members

Subscribe today and get access to all lessons! Plus direct HD download for offline use, enhances transcripts, member comment forums, and iTunes "podcast" RSS feed. Level up your skills now!

Unlock This Lesson

Already subscribed? Sign In


    Using npm run to launch local scripts

    John LindquistJohn Lindquist

    npm run allows you to configure scripts inside of your package.json file which can access locally installed node packages. If you're comfortable with this technique, you can also grunt, gulp, or other build tools by customizing your scripts and saving them inside of your package.json file. With this approach, when a developer starts a new project with your package.json, they can simply run npm install then npm run yourscript without having to install any node packages globally.



    Become a Member to view code

    You must be a Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson


    00:00 If you have a package.json file in your project or you create one using npm init...I'll just accept all the defaults, which will create that'll see that when you open it, you have a little block called "scripts."

    00:12 Anything in that script block can be run from npm run test, which matches up with this name here. When I hit "Enter," you'll see it throws this error, which is expected because it throws an error.

    00:24 If instead I change test to demo...I'll go ahead and change all of this to "echo This is working," hop back into the terminal, and say "npm run demo" this'll see it executes "echo This is working," which outputs "This is working."

    00:42 You can also use this to execute any arbitrary script, such as this test script I have. If I create a new script here...I'll say "test" and then link that up to my script. I can then hop down into the terminal, say "npm run test." You can see it prompts me for my name. I'll say my name is "John." It'll say "Name Entered is John" because that's what this is expecting.

    01:08 The really cool thing is that you'll see if I try and use browserify here and try and get the version number, it says that's an unknown command. If I install browserify locally, create a new script...We'll call this one "version."

    01:23 I'll just execute browserify -v, hop into my terminal, say "npm run version." You can see I can get the version number, 6.3.2, but if I were to try and run that from browserify -v, you can see that it doesn't know what that is.

    01:43 I'll run it from version. You can see it gets that task, which means that I can use npm run to invoke anything I've installed locally without forcing my users to say "npm install -g" to install things globally.

    01:57 Which means with this approach, you could just include a package file in your project, say "npm install" to get everything installed locally, and then npm run whatever task you want to set up, whether it's browserify or whatever. Then it can just grab those locally installed modules and run them.