In this small tutorial we will learn how we use the “npm start” command in node.js.Let us create a simple node application using express framework:
npm install -g express-generator
express mynodeapp
cd mynodeapp/ && npm install // run this command if npm is not installed in the system
PORT=3000 npm start // 3000 is the default port for node.js applications
After running the above command on the terminal the project named “mynodeapp” gets created and will be listening on port 3000 . Now for running node application “mynodeapp” let us install nodemon . Nodemon will make sure if we have made any changes in our project files it will automatically restart our node application this will help us not to run the node command each time after we made the changes in any project file .
npm install -g nodemon
When we run the command npm start from the root directory of our node application, node checks the package.json file for scripts object. If scripts object is found, it checks for a script with the key start and run the command wirtten as a value for the key.
Code inside package.json
{
"name": "mynodeapp",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "node ./bin/www"
},
"dependencies": {
"body-parser": "~1.15.1",
"cookie-parser": "~1.4.3",
"debug": "~2.2.0",
"express": "~4.13.4",
"jade": "~1.11.0",
"morgan": "~1.7.0",
"serve-favicon": "~2.3.0"
}
}
in the above Json “start” is the key and “node ./bin/www” is key's value.
In case package.json do not consist any scripts object or the object do not have a start key, command node server.js is executed instead.
0 Comment(s)