Search code examples
node.jscommand-lineenvironment

node.js process.env not working in command line


I'm taking an online course on writing block-chain from scratch. The course utilizes javascript and node.js. I am very new to these technologies but followed the course so far to the T. i'm pasting the relevant code - the app file (index):

const express = require('express');
const bodyParser = require('body-parser');
const Blockchain = require('../blockchain');
const P2pServer = require('./p2p-server');

const HTTP_PORT = process.env.HTTP_PORT || 3001;

const app = express();
const bc = new Blockchain();
const p2pServer = new P2pServer(bc);

app.use(bodyParser.json());

app.get('/blocks', (req, res) => {
    res.json(bc.chain);
});

app.post('/mine', (req, res) => {
    const block = bc.addBlock(req.body.data);
    console.log(`New blovk added: ${block.toString()}`);

    res.redirect('/blocks');
});

app.listen(HTTP_PORT, () => console.log(`Listening on port ${HTTP_PORT}`));
p2pServer.listen();

and the code from p2p-server.js:

const Websocket = require('ws');

const P2P_PORT = process.env.P2P_PORT || 5001;
const peers = process.env.PEERS ? process.env.PEERS.split(',') : [];
//HTTP_PORT=3002 P2P_PORT=5003 PEERS=ws://localhost:5001 npm run dev

class P2pServer {
    constructor(blockchain) {
        this.blockchain = blockchain;
        this.sockets = [];
    }

    listen() {
        const server = new Websocket.Server({ port: P2P_PORT });
        server.on('connection', socket =>this.connectSocket(socket));

        this.connectToPeers();

        console.log(`listening to peer-to-peer connections on: ${P2P_PORT}`);
    }

    connectToPeers() {
        peers.forEach(peer => {
            const socket = new Websocket(peer);

            socket.on('open', () => this.connectSocket(socket));
        });
    }

    connectSocket(socket){
        this.sockets.push(socket);
        console.log('socket connected');
    }
}

module.exports = P2pServer;

when I try to run the following in the command line:

HTTP_PORT=3002 P2P_PORT=5003 PEERS=ws://localhost:5001 npm run dev

I get the following:

'HTTP_PORT' is not recognized as an internal or external command, operable program or batch file.

for some reason the process.env isn't picking up the input and passing it on to the app. What is wrong here? Thanks!

EDIT: I was asked to add the package.json:

{
  "name": "sf-chain",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "jest --watchAll",
    "dev-test": "nodemon dev-test",
    "start": "node ./app",
    "dev": "nodemon ./app"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "devDependencies": {
    "jest": "^23.1.0",
    "nodemon": "^1.17.5"
  },
  "dependencies": {
    "body-parser": "^1.18.3",
    "crypto-js": "^3.1.9-1",
    "express": "^4.16.3",
    "ws": "^5.2.0"
  }
}

Solution

  • You are using:

    $ HTTP_PORT=3002 P2P_PORT=5003 PEERS=ws://localhost:5001 npm run dev
    

    It is showing an error, because the window powershell will not recognize this command..

    Instead you should use:

    set HTTP_PORT=3002 && set P2P_PORT=5002 && set PEERS=ws://localhost:5001 && npm run dev