go to  ForumEasy.com   
JavaPro  
 
 
   Home  |  MyForum  |  FAQ  |  Archive    You are not logged in. [Login] or [Register]  
Forum Home » JavaScript » NPM Scripts
Email To Friend  |   Set Alert To This Topic Rewarding Points Availabe: 0 (What's this) New Topic  |   Post Reply
Author Topic: NPM Scripts
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 04/22/2021 09:55:59 PM    Edit  |   Quote  |   Report 
NPM Scripts
NPM scripts are scripts defined inside "scripts" object in package.json, which are used to automate repetitive tasks.

 Profile | Reply Points Earned: 0
WebSpider
member
offline   
 
posts: 147
joined: 06/29/2006
from: Seattle, WA
  posted on: 04/22/2021 10:02:54 PM    Edit  |   Quote  |   Report 
Prerequisites
  • Download NodeJS and install it
  • System's PATH should contain C:\Program Files\nodejs
  •  Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 04/22/2021 10:04:49 PM    Edit  |   Quote  |   Report 
    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"
    }
    


     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 04/22/2021 10:07:57 PM    Edit  |   Quote  |   Report 
    Predefined scripts
    Predefined scripts:
    ...
    npm init
    nmp intsall
    npm start
    ...
    

    https://docs.npmjs.com/cli/v7/using-npm/scripts
     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 04/22/2021 10:11:14 PM    Edit  |   Quote  |   Report 
    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!'
    

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 04/22/2021 10:15:30 PM    Edit  |   Quote  |   Report 
    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!'
    

     Profile | Reply Points Earned: 0
    WebSpider
    member
    offline   
     
    posts: 147
    joined: 06/29/2006
    from: Seattle, WA
      posted on: 04/22/2021 10:22:36 PM    Edit  |   Quote  |   Report 
    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!'
    

     Profile | Reply Points Earned: 0

     
    Powered by ForumEasy © 2003-2005, All Rights Reserved. | Privacy Policy | Terms of Use
     
    Get your own forum today. It's easy and free.