简体   繁体   中英

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 :

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. 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. If the file starts with "/", it is considered an absolute path.

https://nodejs.org/en/knowledge/getting-started/what-is-require/

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