“Cannot find module”尽管文件可用

huangapple go评论79阅读模式
英文:

"Cannot find module " despite file being available

问题

I am trying to import my helper functions in my express app index.js. Imported it. Now it is throwing Error

[ERR_MODULE_NOT_FOUND]: Cannot find module 'F:\RnD\express-boilerplate\src\helpers\error' imported from F:\RnD\express-boilerplate\src\index.js

My index.js:

import express from "express";

import { ErrorHandler, handleError } from "./helpers/error";

const app = express();

app.get("/", (req, res) => {
  res.send("hello world");
});

app.get("/error", (req, res) => {
  throw new ErrorHandler(500, "My Bad");
});

app.use((req, res) => {
  throw new ErrorHandler(404, "Not found");
});

app.use((err, req, res, next) => {
  console.log(err.stack);
  handleError(err, res);
});

app.listen(3000, () => {
  console.log("App starting in PORT: 3000");
});

My error.js:

class ErrorHandler extends Error {
  constructor(statusCode, message) {
    super();
    this.statusCode = statusCode;
    this.message = message;
  }
}

const handleError = (err, res) => {
  const { statusCode, message } = err;
  res.status(statusCode).json({
    status: "error",
    statusCode,
    message,
  });
};

export { ErrorHandler, handleError };

I have checked location multiple times. Location is accurate. But why it is showing cannot find module?
I am using node version 19.3.0

英文:

I am trying to import my helper functions in my express app index.js. Imported it. Now it is throwing Error

[ERR_MODULE_NOT_FOUND]: Cannot find module 'F:\RnD\express-boilerplate\src\helpers\error' imported from F:\RnD\express-boilerplate\src\index.js

My index.js:

import express from "express";

import { ErrorHandler, handleError } from "./helpers/error";

const app = express();

app.get("/", (req, res) => {
  res.send("hello world");
});

app.get("/error", (req, res) => {
  throw new ErrorHandler(500, "My Bad");
});

app.use((req, res) => {
  throw new ErrorHandler(404, "Not found");
});

app.use((err, req, res, next) => {
  console.log(err.stack);
  handleError(err, res);
});

app.listen(3000, () => {
  console.log("App starting in PORT: 3000");
});

My error.js:

class ErrorHandler extends Error {
  constructor(statusCode, message) {
    super();
    this.statusCode = statusCode;
    this.message = message;
  }
}

const handleError = (err, res) => {
  const { statusCode, message } = err;
  res.status(statusCode).json({
    status: "error",
    statusCode,
    message,
  });
};

export { ErrorHandler, handleError };

I have checked location multiple times. Location is accurate. But why it is showing cannot find module?
I am using node version 19.3.0

答案1

得分: 0

在主文件的第二行中修改为

import { ErrorHandler, handleError } from "./helpers/error.js";
英文:

in the 2nd line of main file make it

import { ErrorHandler, handleError } from "./helpers/error.js";

答案2

得分: 0

这是因为EcmaScript的行为,你需要指定文件扩展名,更多信息请查阅nodejs.org

在index.js的第二行更新,你正在导入模块如下:

import { ErrorHandler, handleError } from "./helpers/error.js";
英文:

This is because of the behavior of EcmaScript, you need to specify the file extension, read more about it here nodejs.org.

Update the second line of index.js, where you are importing modules as

import { ErrorHandler, handleError } from "./helpers/error.js";

huangapple
  • 本文由 发表于 2023年7月18日 16:49:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76711008.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定