英文:
"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";
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论