0
votes

I am building an Angular app connected to a Node/Express backend. Even after successfully installing node, nodemon and express in my project, I am getting the following error when trying to start my node server with npm run start:server

enter image description here

Note that the Angular code is running properly with the ng serve command

enter image description here

Here is my package.json

  "name": "mean",
  "version": "0.0.0",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e",
    "start:server": "nodemon server.js"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "~10.0.3",
    "@angular/cdk": "~10.0.2",
    "@angular/common": "~10.0.3",
    "@angular/compiler": "~10.0.3",
    "@angular/core": "~10.0.3",
    "@angular/forms": "~10.0.3",
    "@angular/material": "^10.0.2",
    "@angular/platform-browser": "~10.0.3",
    "@angular/platform-browser-dynamic": "~10.0.3",
    "@angular/router": "~10.0.3",
    "express": "^4.17.1",
    "rxjs": "~6.6.0",
    "tslib": "^2.0.0",
    "zone.js": "~0.10.3"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "~0.1000.2",
    "@angular/cli": "~10.0.2",
    "@angular/compiler-cli": "~10.0.3",
    "@angular/language-service": "~10.0.3",
    "@types/jasmine": "~3.3.8",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "codelyzer": "^6.0.0",
    "jasmine-core": "~3.5.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~5.0.0",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~3.3.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "nodemon": "^2.0.4",
    "protractor": "~7.0.0",
    "ts-node": "~7.0.0",
    "tslint": "~6.1.0",
    "typescript": "~3.9.6"
  }
}

Here is my server.js

const app = require("./backend/app");
const debug = require("debug")("node-angular");
const http = require("http");

const normalizePort = val => {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
};

//Check which type of errors occur
const onError = error => {
  if (error.syscall !== "listen") {
    throw error;
  }
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  switch (error.code) {
    case "EACCES":
      console.error(bind + " requires elevated privileges");
      process.exit(1);
      break;
    case "EADDRINUSE":
      console.error(bind + " is already in use");
      process.exit(1);
      break;
    default:
      throw error;
  }
};

const onListening = () => {
  const addr = server.address();
  const bind = typeof port === "string" ? "pipe " + port : "port " + port;
  debug("Listening on " + bind);
};

const port = normalizePort(process.env.PORT || "3001");
app.set("port", port);

const server = http.createServer(app);
server.on("error", onError);
server.on("listening", onListening);
server.listen(port);

here is my app.js


const app = express();

app.use((req,res,next) =>{
  console.log('First middleware');
   next();
});

app.use((req,res,next) =>{
  res.send('Hello from Express!');
});

module.exports = app;
1
Just as a sanity-check: Did you remove the entire node_modules folder and ran npm | yarn install afterwards to get a fresh install of all required packages? - Philipp Meissner
what is ./backend/app ? can you also post the code for that ? - pbachman
@PhilippMeissner I removed the node_modules and ran npm install but I still got the same error - olivier Noumbi
@pbachman Posted - olivier Noumbi
@olivierNoumbi what node version do you use ? your code works, i think it has to do with your node_modules folder. please delete it and install the dependencies again. - pbachman

1 Answers

0
votes

In your package.json, use

"start:server": "npx nodemon server.js"

It will work.