javascriptnode.jsexpressroutesmodulenotfounderror

Local Module Not Found in Node.js


In Node.js, I created my own custom module called apiEndpoints.js. However, when I try to access it, I get a Cannot find module error by Node.js. Below is my Server.js code:

import express from "express";
import mongoose from "mongoose";
import Cors from "cors";
import apiEndpoints from "./routes/apiEndpoints";

// Initialization
const app = express();
const port = process.env.port || 80;
const connection_url =
  "mongodb+srv://admin:EUl8KIA9dLv7qXKa@cluster0.mblf7.mongodb.net/appDB?retryWrites=true&w=majority";

// Middleware
app.use(express.json());
app.use(Cors());

// DB Config
mongoose.connect(connection_url);

// API Endpoints
app.use(apiEndpoints);

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

Below is my apiEndpoints.js code:

import express from "express";
import Users from "../models/users";

const router = express.Router();

router.get("/users", (req, res) => {
  Users.find((err, data) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send(data);
    }
  });
});

router.get("/user/:username", (req, res) => {
  const username = req.params.username;

  Users.findOne({ username: username }, (err, data) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send(data);
    }
  });
});

router.post("/user/create", (req, res) => {
  const user = req.body;

  Users.create(user, (err, data) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(201).send(data);
    }
  });
});

router.put("/user/:username", (req, res) => {
  const username = req.params.username;
  const updatedData = req.body;

  Users.findOneAndUpdate(
    { username: username },
    updatedData,
    { new: true },
    (err, data) => {
      if (err) {
        res.status(500).send(err);
      } else {
        res.status(200).send(data);
      }
    }
  );
});

router.delete("/user/delete/:username", (req, res) => {
  const username = req.params.username;

  Users.findOneAndDelete({ username: username }, (err, data) => {
    if (err) {
      res.status(500).send(err);
    } else {
      res.status(200).send(data);
    }
  });
});

export default router;

And, this is my folder structure:
Folder Structure

Also, below, is my package.json file:

{
  "name": "api",
  "version": "1.0.0",
  "description": "",
  "main": "server.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node server.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "mongoose": "^6.0.4"
  }
}

So my question is why is it saying "Module not Found" when Server.js tries to access apiEndpoints.js?


Solution

  • I eventually solved my problem. My problem was that I didn't set up the es6 for Node.js properly. This tutorial on Medium helped me set up es6 properly by using Babel. I then added the .js file extension on all of my imports, which works.