[Whole Web] [Node.js] Using npm run to launch local scripts

时间:2023-12-26 16:27:02

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 runnpm install then npm run yourscript without having to install any node packages globally.

For example:

If you haven't installed browserify globally, and you use npm to install it locallly:

npm install browserify --save-dev

Then you create a test script to run browserify:

  "scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"demo": "echo Hello World!",
"b_version": "browserify -v"
},

You run on tml:

npm run b_version

You will get the version number, but if you type in tml:

browserify -v

You will get error, because you haven't installed it globally, 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.

More:

https://egghead.io/lessons/nodejs-using-npm-run-to-launch-local-scripts#/tab-transcript