Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute a module immediately after calling import in ES6 [duplicate]

I'm working on something on NodeJS and I'm using the import keyword from the ES6 syntax.. and I want to execute immediately after calling it. I searched for similar thoughts to do that but nothing was helpful enough.

What I want to do is basically transform the following code from the CommonJS into the ES6.

// In CommonJS:
var birds = require('./birds')()

// In ES6:
import birds from './birds'()

I can do that using the const keyword:

import birds from './birds'
const SomethingButNotBirds = birds()

But, I really wanna know if there's a better way to do it.

I really appreciate your help guys!

like image 998
YahiaRefaiea Avatar asked Feb 03 '18 02:02

YahiaRefaiea


Video Answer


1 Answers

ES6 import statement has a declarative syntax and does not give you room to execute functions just as you'd do with require()().

The code you have below is the only valid way of doing it.

import birds from './birds'
const SomethingButNotBirds = birds()
like image 193
fortunee Avatar answered Nov 11 '22 18:11

fortunee