Scripts

What are NPM Scripts

{   
    "name": "super-cool-package",   
    "version": "1.0.0",   
    "scripts": {    
        "start": "node src/index.js"  
    }, 
    "dependencies": { 
        ...
    }    
    "devDependencies": {     
        ...   
    } 
}

npm pre, post

{   
    "name": "super-cool-package",   
    "version": "1.0.0",   
    "scripts": {    
        "prepack": "run BEFORE a tarball is packed (on npm pack, npm publish, and when installing git dependencies)",
        "postpack": "Run AFTER the tarball has been generated and moved to its final destination.",
        // PUBLISH
        "publish": "", 
        "postpublish": "Run AFTER the package is published.",
        // INSTALL
        "preinstall": "Run BEFORE the package is installed",
        "install": "", 
        "postinstall": "Run AFTER the package is installed.",
        "preuninstall": "Run BEFORE the package is uninstalled.", 
        "uninstall": "Run BEFORE the package is uninstalled.",
        "postuninstall": "Run AFTER the package is uninstalled",
        // VERSION
        "preversion": "Run BEFORE bumping the package version.",
        "version": "Run AFTER bumping the package version, but BEFORE commit.",
        "postversion": "Run AFTER bumping the package version, and AFTER commit.",
        // TEST
        "pretest":"", 
        "test":"", 
        "posttest": "Run by the npm test command.",
        // STOP
        "prestop":"", 
        "stop":"", 
        "poststop": "Run by the npm stop command.",
        // START
        prestart":"", 
        "start":"", 
        "poststart": "Run by the npm start command.",
        // RESTART
        "prerestart":"", 
        "restart":"", 
        "postrestart": "Run by the npm restart command. Note: npm restart will run the stop and start scripts if no restart script is provided.",
        "preshrinkwrap":"", 
        "shrinkwrap":"", 
        "postshrinkwrap": "Run by the npm shrinkwrap command.",
    }, 
    "dependencies": { 
        ...
    }    
    "devDependencies": {     
        ...   
    } 
}

npm run script

 
...
"scripts": {
    "start": "node index.js",
    "hello": "echo 'Hello World!'"
}
...

$ npm run hello
Hello World!

npm script examples

 
...
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "deploy": "echo 'deploying...' && zip -r lambda.zip . && aws lambda update-function-code --function-name bluebot-alexa-skill --zip-file fileb://lambda.zip --profile dddc && rm -rf lambda.zip && echo 'deploy complete'"
  }
... 

$ npm run hello
$ "echo 'deploying...'
$ zip -r lambda.zip . 
$ aws lambda update-function-code --function-name bluebot-alexa-skill --zip-file fileb://lambda.zip --profile dddc
$ rm -rf lambda.zip
$ echo 'deploy complete'"

npm script examples

 
...
  "scripts": {
    "start": "sudo PORT=80 react-scripts start",
    "test": "react-scripts test --env=jsdom --coverage --collectCoverageFrom=**/components/*",
    "testnocoverage": "react-scripts test --env=jsdom",
    "prettier": "prettier \"**/*.+(js|jsx|json|css|less|scss|sass|graphql)\"",
    "format": "npm run prettier -- --write",
    "validate": "npm run lint && npm run prettier -- --list-different",
    "precommit": "lint-staged",
    "swagger": "sh scripts/swagger.sh"
  },
...

npm script examples

# Custom branch argument
branch=$1
if [ $# -eq 0 ]
  then
    branch="develop"
fi

# Remove current swagger.json
rm -rf src/assets/*.swagger.json

# Make sure documentation is up to date
cd ../documentation/apis
git checkout $branch
git pull

# Generate
./node_modules/yamljs/bin/yaml2json -psr .
mv *.swagger.json ../../frontend/src/assets/

# Return to project
cd ../../frontend

NPM Scritpts

By Jd Fiscus

NPM Scritpts

Understanding NPM scripts

  • 268
Loading comments...

More from Jd Fiscus