简体   繁体   中英

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:
文件夹结构

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 ?

Okay, so after hours of hard-work, I was able to solve my problem. So what my problem was that I didn't setup the es6 for Node.js properly. So, I followed along this tutorial on Medium, and was able to setup of es6 properly by using Babel. Then, I added the .js file extension on all of my imports, and it works.

You need to add the file extension to all import and export statements then it will work without Babel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM