
Download the Node.js installer from https://nodejs.org/en/download and follow the prompts to install Node.js on Windows. Open VSCode and the node --version command should show the version of Node.js that was installed.
C:\Users\JohnDoe> node --version
v18.16.0
Create a file named app.js that contains the following console.log.
var msg = "Hello World";
console.log(msg);
In Terminal, run app.js and "Hello World" should be displayed.
C:\Users\JohnDoe> node app.js
Hello World
Let's create a file named package.json that contains the following.
{
"dependencies": {
"express": "^4.18.2"
},
"scripts": {
"start": "node app.js"
},
"type": "commonjs"
}
The npm (Node Package Manager) install command will install the dependencies in package.json. In other words, the npm install command will install the "express" package.
C:\Users\JohnDoe> npm install
The npm list command should now show that express is installed.
C:\Users\JohnDoe>
node@ C:\Users\JohnDoe\node
├── express@4.18.2
Let's update app.js to have the following. Notice in this example that package.json has "type": "commonjs". WIth CommonJS, you will use require.
const express = require("express")
const app = express()
app.get('/', (req, res) => {
console.log('Hello Console')
res.send('Hello World')
})
app.listen(12345)
If package.json had "type": "module" then app.js would have to use import (not require).
import express from "express";
const app = express()
app.get('/', (req, res) => {
console.log('Hello Console')
res.send('Hello World')
})
app.listen(12345)
Then run app.js again. Because we added "script start node app.js" to package.json we can now use npm start to run app.js.
C:\Users\JohnDoe> npm start
> start
> node app.js
Go to http://127.0.0.1:12345 and "Hello World" should be displayed and "Hello Console" should be displayed in the VSCode console.
Did you find this article helpful?
If so, consider buying me a coffee over at