close

How do I add a custom script to my package.json file that runs a javascript file?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How do I add a custom script to my package.json file that runs a javascript file? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How do I add a custom script to my package.json file that runs a javascript file ?

  1. How do I add a custom script to my package.json file that runs a javascript file ?

    do I add a custom script to my package.json file that runs a javascript file Lets say in scripts you want to run 2 commands with a single command:
    "scripts":{ "start":"any command", "singleCommandToRunTwoCommand":"some command here && npm start" }
    Now go to your terminal and run there npm run singleCommandToRunTwoCommand.

  2. How do I add a custom script to my package.json file that runs a javascript file ?

    do I add a custom script to my package.json file that runs a javascript file Lets say in scripts you want to run 2 commands with a single command:
    "scripts":{ "start":"any command", "singleCommandToRunTwoCommand":"some command here && npm start" }
    Now go to your terminal and run there npm run singleCommandToRunTwoCommand.

Method 1

I have created the following, and it’s working on my system. Please try this:

package.json:

{
  "name": "test app",
  "version": "1.0.0",
  "scripts": {
    "start": "node script1.js"   
  }
}

script1.js:

console.log('testing')

From your command line run the following command:

npm start

Additional use case

My package.json file has generally the following scripts, which enable me to watch my files for typescript, sass compilations and running a server as well.

 "scripts": {
    "start": "concurrently \"sass --watch ./style/sass:./style/css\" \"npm run tsc:w\" \"npm run lite\" ",    
    "tsc": "tsc",
    "tsc:w": "tsc -w", 
    "lite": "lite-server",
    "typings": "typings",
    "postinstall": "typings install" 
  }

Method 2

Lets say in scripts you want to run 2 commands with a single command:

"scripts":{
  "start":"any command",
  "singleCommandToRunTwoCommand":"some command here && npm start"
}

Now go to your terminal and run there npm run singleCommandToRunTwoCommand.

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment