Changing the npm start-script of Node.js


The start-script of a Node.js application consists of all the commands that will be used to perform the specific tasks. When starting or initializing a Node.js project, there are a lot of predefined scripts that will be created for running the application. These scripts can be changed as per the need or demand of the project.

Script commands are widely used for making different startup scripts of programs in both Node and React. 'npm start' is used for executing a startup script without typing its execution command.

Package.json File

This is the start-up script that needs to be added in the package.json file. The name of the file here is 'index.js'. You can change this name as per your needs.

"scripts"{
   "start":"node index.js" //Name of the file
}

Below is an illustartion showing the implmentation of startup script.

Example - sendStatus()

Create a file with name – index.js and copy the below code snippet. After creating the file, use the below command to run this code as shown in the example below −

npm start

index.js

// Importing the express module
const express = require('express');
const app = express();

// Initializing the data with the following string
var data = "Welcome to TutorialsPoint !"

// Sending the response for '/' path
app.get('/' , (req,res)=>{

   // Sending the data json text
   res.send(data);
})

// Setting up the server at port 3000
app.listen(3000 , ()=>{
   console.log("server running");
});

Output

C:\home
ode>> npm start

The above statement will itseld run the index.js file. This command itself starts the main class for the project.

Now hit the following URL from your browser to access the webpage – http://localhost:3000

Updated on: 20-May-2021

9K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements