js exports code example

Example 1: import and export type in js

// Exporting individual features
export let name1, name2,, nameN; // also var, const
export let name1 =, name2 =,, nameN; // also var, const
export function functionName(){...}
export class ClassName {...}

// Export list
export { name1, name2,, nameN };

// Renaming exports
export { variable1 as name1, variable2 as name2,, nameN };

// Exporting destructured assignments with renaming
export const { name1, name2: bar } = o;

// Default exports
export default expression;
export default function () {} // also class, function*
export default function name1() {} // also class, function*
export { name1 as default,};

// Aggregating modules
export * from; // does not set the default export
export * as name1 from; // Draft ECMAScript® 2O21
export { name1, name2,, nameN } from;
export { import1 as name1, import2 as name2,, nameN } from;
export { default } from;

Example 2: how to export module in node js

module.exports ={
 //functions
}

Example 3: module.exports vs exports

//module.exports used for object 
var log = {
            info: function (info) { 
                console.log('Info: ' + info);
            },
            warning:function (warning) { 
                console.log('Warning: ' + warning);
            },
            error:function (error) { 
                console.log('Error: ' + error);
            }
    };

module.exports = log

//exports used for function 

exports.SimpleMessage = 'Hello world';

Example 4: export function javascript

// module "mon-module.js"
export default function cube(x) {
  return x * x * x;
}

Example 5: module.exports equivalent es6

exports["default"] = A; module.exports = exports["default"];