Linux localhost Node

Ubuntu 20.04 Node Server on localhost with SSL

To get SSL on your localhost for testing purposes you will need a SSL key and certificate generated. I do the following in a certs directory to call up later in the node server app.

openssl genrsa -des3 -out rootCA.key 2048
openssl req -x509 -new -nodes -key rootCA.key -sha256 -days 1024 -out rootCA.pem

Import the rootCA.pem file into your browser under the ‘Authority‘ tab.

Then create server.cnf as follows

default_bits = 2048
prompt = no
default_md = sha256
distinguished_name = dn

CN = localhost

… and v3.ext as follows

keyUsage = digitalSignature, nonRepudiation, keyEncipherment, dataEncipherment
subjectAltName = @alt_names

DNS.1 = localhost

Using the above config files you can create the server key and certificate with the following

openssl req -new -sha256 -nodes -out server.csr -newkey rsa:2048 -keyout server.key -config <( cat server.csr.cnf )

openssl x509 -req -in server.csr -CA rootCA.pem -CAkey rootCA.key -CAcreateserial -out server.crt -days 500 -sha256 -extfile v3.ext

Now you can start the server with npm start or node server.js, with server.js as follows (basic example)

const express = require('express')
const app = express()
const https = require('https')
const fs = require('fs')
const port = 3000

app.get('/', (req, res) => {

const httpsOptions = {
  key: fs.readFileSync('./certs/server.key'),
  cert: fs.readFileSync('./certs/server.csr')
const server = https.createServer(httpsOptions, app).listen(port, () => {
  console.log('server running at ' + port)

Open your browser and go to https://localhost:3000 and all should be good.

Linux Node npm

Installing Node on Ubuntu 20.04

Step 1 Do the usual first, i.e.

sudo apt update


sudo apt upgrade

Step 2 Make sure npm is up to date as well

npm cache clean -f
npm install -g n #if not already installed
sudo n stable

It might take some doing to achieve the above, as I had a few issues to deal with ion Ubuntu 20..04 Firstly I had to update npm and node :

curl -fsSL | sudo -E bash -
sudo apt-get install -y nodejs

Then I had to change the permissions on the node modules directory

sudo chown -R $USER /usr/local/lib/node_modules

Only then could I get sudo npm install -g to work as intended.