Using npm run to launch local scripts

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 9 years ago
Updated 5 years ago

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.

[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 file...you'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 time...you'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.

egghead
egghead
~ 28 minutes ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today