简体   繁体   中英

Nodejs - functions becomes undefined after exporting

I have this on my app.js and I am exporting the foo object.

...some code

exports.foo = {
    bar: function(){
        return 'this is a test'
    }
}

...some code

Then on my other test.js file I am importing it like.

import foo from './path/to/app';

But when I tried to use foo.bar() I am getting this error TypeError: _app2.default.bar is not a function

Can someone explain what's happening here?

use this syntax

module.exports = {
        bar: function(){
            return 'this is a test'
        }
    }


var foo = require("./pathToFoo.js");

foo.bar();

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