提问者:小点点

Node TypeError-require()d类方法上的“not a function”


我有以下类:

const exec = require('child_process').execSync;

class Git {

    static branchExists (branchNameToCheck) {
        return ( exec(`git rev-parse --verify --quiet ${branchNameToCheck} > /dev/null`).length > 0 );
    }

}

module.export = Git;

这在以下(简化)脚本中调用:

#!/usr/bin/env node

const Git = require('./classes/Git');

if (Git.branchExists('develop')) console.log('success');

出现以下错误:

Git.branchExists();
    ^

TypeError: Git.branchExists is not a function

为什么它不将BranchExists识别为函数? 当我运行console.log(Git)时,我得到{}


共1个答案

匿名用户

我认为它可能只是一个错误:module.export=git;->; module.exports=git;。 如果不定义module.exports,导入的对象(在本例中为“git”)将是一个空对象https://stackabuse.com/how-to-use-module-export-in-node-js/