Can I get some help on what am I missing to finish a server.js for REACT?

I think I got all the dependencies and code, but obviously I am missing something because the console isn’t logging “Server is running on port 5000”…

This is the code from my server.js

const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");

require("dotenv").config();

const app = express();
const port = process.env.PORT || 5000;

app.use(cors());
app.use(express.json());

const uri = process.env.ATLAS_URI;
mongoose.connect(uri, { useNewUrlParser: true, useCreateIndex: true });

app.listen(port, () => {
  console.log(`Server is running on port: ${port}`);
});

This is from my package.json

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node backend/server.js",
    "server": "nodemon backend/server.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "D": "^1.0.0",
    "dotenv": "^16.0.0",
    "express": "^4.17.2",
    "mongoose": "^6.2.1"
  },
  "devDependencies": {
    "nodemon": "^2.0.15"
  }
}

This is on my .env file

ATLAS_URI=mongodb+srv://myusername:mypassword@cluster0.gcyyo.mongodb.net/test?retryWrites=true&w=majority
PORT=5000

This is what I am getting in the console.

npm rs
rs
[nodemon] starting node backend/server.js index.js
node:internal/modules/cjs/loader:936
throw err;
^

Error: Cannot find module ‘C:\users\edgar\desktop\stuff\acode\table\testb\backend\index.js’
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
at node:internal/main/run_main_module:17:47 {
code: ‘MODULE_NOT_FOUND’,
requireStack:
}
[nodemon] app crashed - waiting for file changes before starting…

In package.json you have "main": "index.js" which is what node tries to run when you execute npm start.

1 Like

Yes… under package.json … I changed to “main”: “server.js”, then saved it, and restarted it and it worked.

now I got a another error dealing with mongoDB connection… some sort of authentication failed that I am working on…