
The npm start CLI and scripts start in package.json can be used to start a Node.js app. For example, let's create a temporary directory (on a Linux system).
mkdir /tmp/foo
And move into the temporary directory.
cd /tmp/foo
And create a file named app.js that contains the following.
console.log("Hello World");
Of course, you could just use the node CLI to run app.json.
]$ node app.js
Hello World
Or you could use the nodemon (node monitor) CLI. Check out my article FreeKB - Node.js - Getting Started with nodemon.
~]$ nodemon app.js
[nodemon] 3.1.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node app.js`
Hello World
[nodemon] clean exit - waiting for changes before restart
Another option would be to use the npm start (Node.js Package Manager) CLI and start scripts in package.json. This assumes you have the npm CLI installed. If not, check out my article FreeKB - Node.js - Install Node.js on Linux.
Let's say you have the following in package.json.
{
"scripts": {
"start": "node app.js"
}
}
The npm start package.json command should return something like this, basically running the node app.js command.
]$ npm start package.json
> start
> node app.js package.json
Hello World
Or if you had nodemon in package.json. In this scenario, you would probably also have "dependencies" to ensure a minimum version of nodemon is installed on the system.
{
"scripts": {
"start": "nodemon app.js"
},
"dependencies": {
"nodemon": "^3.0.1"
}
}
The npm start package.json command should return something like this, basically running the nodemon app.js command.
]$ npm start package.json
> start
> nodemon app.js package.json
[nodemon] 3.1.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node app.js package.json`
Hello World
[nodemon] clean exit - waiting for changes before restart
Did you find this article helpful?
If so, consider buying me a coffee over at