[英]NodeJS cannot find module
I have entities directory that store user.js file like this: 我有存储这样的user.js文件的实体目录:
"use strict";
class User {}
module.exports = User;
and in my index.js: 在我的index.js中:
var User = require("./entities/User")
but I got an error 但我有一个错误
Error: Cannot find module './entities/User'
错误:找不到模块“ ./entities/User”
at Function.Module._resolveFilename (module.js:470:15)在Function.Module._resolveFilename(module.js:470:15)
at Function.Module._load (module.js:418:25)在Function.Module._load(module.js:418:25)
at Module.require (module.js:498:17)在Module.require(module.js:498:17)
at require (internal/module.js:20:19)在要求时(internal / module.js:20:19)
at Socket.socket.on (/home/etours/capstone-etours/index.js:154:21)在Socket.socket.on(/home/etours/capstone-etours/index.js:154:21)
at emitTwo (events.js:106:13)在emitTwo(events.js:106:13)
at Socket.emit (events.js:194:7)在Socket.emit(events.js:194:7)
at /home/etours/capstone-etours/node_modules/socket.io/lib/socket.js:503:12在/home/etours/capstone-etours/node_modules/socket.io/lib/socket.js:503:12
at _combinedTickCallback (internal/process/next_tick.js:73:7)在_combinedTickCallback(内部/进程/next_tick.js:73:7)
at process._tickCallback (internal/process/next_tick.js:104:9)在process._tickCallback(内部/进程/next_tick.js:104:9)
The require function it's case sensitive so you have to write the path exactly how you created. require函数区分大小写,因此您必须完全按照创建方式编写路径。
if you use the U
lowercase it's gonna work :D 如果您使用
U
小写字母,它将起作用:D
You should try this code: 您应该尝试以下代码:
user.js user.js的
var User = function() {};
module.exports = User;
var User = class User {
};
module.exports.User = User;
index.js index.js
var User = require("./user");
console.log(User);
You can also try below code: 您也可以尝试以下代码:
user.js: user.js的:
var User={};
module.exports = User;
var User = class User {
};
module.exports.User = User;
index.js: index.js:
var User = require("./user");
console.log(User);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.