How to change npm start script of node.js ?
The script is a list of commands that are used to perform some specific tasks that they are designed to perform. In Node.js there are a lot of predefined scripts created while creating a new project some of them contain a default value, and we can also change them according to our need of the operations for a specific task.
Script keywords available in node.js are given below npm start script is one of the most commonly used scripts in the node.js as well as in react. npm start: npm start script is used to execute the defined file in it without typing its execution command.
Package.json file
"scripts"{
"start":"node index.js"
}
index.js
const http = require( "http" )
const server = http.createServer((req,res)=>{
req.statusCode=200;
console.log( "Server is Started" )
res.end();
});
server.listen(3000, "localhost" ,()=>{
console.log( "Server is Running " )
})
|
Run index.js file using below command:
node index.js
Output:
When no execution file mentioned in the npm start script npm automatically runs the node server.js file if available in the project directory.
package.json
server.js
const http = require( "http" )
const server = http.createServer((req,res)=>{
req.statusCode=200;
console.log( "Server is Executed" )
res.end();
});
server.listen(3000, "localhost" ,()=>{
console.log( "This is server.js file" )
})
|
Run server.js file using below command:
node server.js
Output:
Last Updated :
31 Mar, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...