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.
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."
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.
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."
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.
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."
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.
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.
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.