Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is ES6 equivalent of module.exports = { key: "value" }?

I have the following code:

module.exports  = { 
    key: "value",
    key2: 1234
}

If I change it to:

export default {
    key: "value",
    key2: 1234
}

Then the following import stops working:

import {key, key2} from 'module.js';

What is an ES6 equivalent of exporting an object?

like image 781
splattru Avatar asked Jun 08 '17 15:06

splattru


People also ask

What is the equivalent of module exports in ES6?

All CommonJS and AMD modules are presented to ES6 as having a default export, which is the same thing that you would get if you asked require() for that module—that is, the exports object. ES6 modules were designed to let you export multiple things, but for existing CommonJS modules, the default export is all you get.

Is module exports 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 export keyword in JavaScript?

The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import.

What is a module in ES6?

Modules are the piece or chunk of a JavaScript code written in a file. JavaScript modules help us to modularize the code simply by partitioning the entire code into modules that can be imported from anywhere. Modules make it easy to maintain the code, debug the code, and reuse the piece of code.


1 Answers

You can first define the variables and export them:

const key = 'value';
const key2 = 1234;

export { key, key2 };

Or you can export them in the same line in which you define them:

export const key = 'value';
export const key2 = 1234;
like image 155
Michał Perłakowski Avatar answered Sep 23 '22 02:09

Michał Perłakowski