Author |
Topic: NPM Scripts |
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
NPM Scripts |
NPM scripts are scripts defined inside "scripts" object in package.json, which are used to automate repetitive tasks.
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Prerequisites |
Download NodeJS and install it System's PATH should contain C:\Program Files\nodejs
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Installation |
C:\tmp\npm-scripts\hello>npm init -y
Wrote to C:\tmp\npm-scripts\hello\package.json:
{
"name": "hello",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Predefined scripts |
Predefined scripts:
...
npm init
nmp intsall
npm start
...
https://docs.npmjs.com/cli/v7/using-npm/scripts
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Custom scripts |
package.json:
...
"scripts": {
"start": "node index.js",
"hello": "echo 'Hello World!'"
}
...
NOTE: The custom scripts must be executed by adding run like:
C:\tmp\npm-scripts\hello>npm run hello
> hello@1.0.0 hello C:\tmp\npm-scripts\hello
> echo 'Hello World!'
'Hello World!'
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Scripts calling scripts |
package.json:
...
"scripts": {
"start": "node index.js",
"hello": "echo 'Hello World!'"
"calling-hello": "npm run hello",
}
...
C:\tmp\npm-scripts\hello>npm run calling-hello
> hello@1.0.0 calling-hello C:\tmp\npm-scripts\hello
> npm run hello
> hello@1.0.0 hello C:\tmp\npm-scripts\hello
> echo 'Hello World!'
'Hello World!'
|
|
|
|
|
|
|
WebSpider member offline |
|
posts: |
147 |
joined: |
06/29/2006 |
from: |
Seattle, WA |
|
|
|
|
|
Scripts chain-calling |
package.json:
...
"scripts": {
"start": "node index.js",
"hello": "echo 'Hello World!'",
"calling-hello": "npm run hello",
"calling-hello-and": "npm run hello && echo 'Hello NPM!'"
}
...
C:\tmp\npm-scripts\hello>npm run calling-hello-and
> hello@1.0.0 calling-hello-and C:\tmp\npm-scripts\hello
> npm run hello && echo 'Hello NPM!'
> hello@1.0.0 hello C:\tmp\npm-scripts\hello
> echo 'Hello World!'
'Hello World!'
'Hello NPM!'
|
|
|
|
|
|
|