Migrating es5 export to es6 by default

I am trying to port code from es5 to es6, I am completely new in both cases, if someone can help me, I would be very grateful.

es5 version:

lib.js

module.exports = {
    foo1: function () { 
        this.foo2() {
           ... 
        }
    },
    foo2: function () { 
        ...
    }
}

      

main.js

const Lib = require("./lib");
Lib.foo1( { ... });

      

es6 version - i am trying:

lib.ts

export default { 
    foo1() {
        this.foo2(() => {
            ... 
        });                 
    },
    foo2(){ ... }
}

      

main.ts

import * as Lib from "./lib";
Lib.foo1({ ... })

      

The problem in my main.ts foo1 cannot be resolved. Any idea or recommendation?

Thank!

+3


source to share


2 answers


It should be easy

import Lib from "./lib";

      



Otherwise, if you are using the notation * as

, you can access default

export with Lib.default

, but this is optional.

+2


source


I don't understand the following part of your code:

foo1: function () { 
    this.foo2() {
       ... 
    }
}

      

This seems to be invalid.

In any case, don't introduce your own pseudo-modular structure. It is not necessary. lib.js

is already a module.



lib.js

export function foo1() {
    foo2();
}
export function foo2() { ... }

      

main.js

import {foo, foo2} from './lib';

      

+1


source







All Articles