Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would I translate this require to ES6 import style [duplicate]

I would like to do this

var debug = require('debug')('myapp');

... in ES6 without creating an extra variable. Can it be done?

like image 508
Thijs Koerselman Avatar asked Jan 28 '15 17:01

Thijs Koerselman


People also ask

How is require () different from import ()?

One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() cannot be called conditionally, it always runs at the beginning of the file. To use the require() statement, a module must be saved with .

How do I import and export ES6?

With the help of ES6, we can create modules in JavaScript. In a module, there can be classes, functions, variables, and objects as well. To make all these available in another file, we can use export and import. The export and import are the keywords used for exporting and importing one or more members in a module.

What is import in ES6?

What is ES6 import? ES6 stands for version 6 of the ECMA Script programming language and an import statement is used to import modules that are exported by some other module. A module is an encapsulated file that meets up with an external application on the basis of its related functionality.


1 Answers

import Debug from 'debug';

const debug = Debug('myapp');

(as lemieuxster said... addressing the fact that it is still listed under unanswered questions)

Note as mentioned in the comments, this will work for modules exported with the es6 syntax, that is whenever export default expression was used, which would give way to a require of the form var debug = require('./debug').default('myapp');. If the module you are importing used an export syntax of the type export const Debug = expression or export {Debug} or module.exports = {Debug : expression} then you will have to use import {Debug} from 'debug';

like image 107
widged Avatar answered Oct 03 '22 19:10

widged