I would like to do this
var debug = require('debug')('myapp');
... in ES6 without creating an extra variable. Can it be done?
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 .
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 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.
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';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With