Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

import and call a function with es6 [duplicate]

Previously:

var debug = require('debug')('http')
  , http = require('http')
  , name = 'My App';

With es6, how can I import and invoke right away like the first line?

import debug from 'debug'();

is a no no?

like image 470
Mohamed El Mahallawy Avatar asked Jul 23 '15 23:07

Mohamed El Mahallawy


2 Answers

You'll need two lines:

import debugModule from 'debug';
const debug = debugModule('http');

The import syntax is a declarative import syntax, it does not execute any functions.

like image 149
loganfsmyth Avatar answered Nov 15 '22 18:11

loganfsmyth


is a no no?

Correct. Keep in mind that the import statement is analogous to more than a simple require() statement -- it also creates a binding of the "loaded" module to a local variable.

That is,

import debug from 'debug'();

...is more close in behavior/semantics to

var debug = require('debug');

...than it is to simply

require('debug');

Analogies to commonjs-style module loaders will obviously break down at some point, but at the end of the day it's a "no no" due to the plain and simple fact that import debug from 'debug' doesn't actually resolve to anything that you can invoke (or otherwise reference).

like image 4
jmar777 Avatar answered Nov 15 '22 19:11

jmar777