[英]Node Cannot find module for custom class
I've separated some of my methods from my main script into another file:我已经将我的一些方法从我的主脚本中分离到另一个文件中:
const { GitController } = require('controllers/GitController');
class Controller
{
constructor() {
this.git = new GitController();
this.git.initialize().then(() => {
});
controllers/GitController.js
: controllers/GitController.js
:
const git = require('gulp-git');
const fs = require('fs');
const util = require('util');
class GitController
{
constructor() {}
}
However i'm getting the following error:但是我收到以下错误:
> node server.js
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module 'controllers/GitController'
The path in your require
statement is absolute. require
语句中的路径是绝对的。 It should probably be relative, like:它可能应该是相对的,例如:
const { GitController } = require('./controller/GitController');
From official tutorial:来自官方教程:
If the file starts with "./" it is considered a relative file to the file that called require.
如果文件以“./”开头,则它被认为是调用 require 的文件的相对文件。 If the file starts with "/", it is considered an absolute path.
如果文件以“/”开头,则认为它是绝对路径。
https://nodejs.org/en/knowledge/getting-started/what-is-require/ https://nodejs.org/en/knowledge/getting-started/what-is-require/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.