vijay
vijay

Reputation: 11027

How to use javascript export keyword in browser or console

export keyword is introduced in ecmascript5:

var myFunc1 = function() { console.log('hello'); };   
export.myFunc1 = myFunc1;

If I run above code in firefox console it gives error:

SyntaxError: missing declaration after 'export' keyword  
export.myFunc1 = myFunc1;

I don't understand what I need to declare.

Am I using it in the wrong way?

Any advice would be nice!

Upvotes: 1

Views: 5702

Answers (2)

corn3lius
corn3lius

Reputation: 5005

Node js uses exports to expose functionality in modules to their implementers

defined here. https://nodejs.org/api/modules.html#modules_module_exports

Is this what you're trying to do?

Upvotes: 0

Trott
Trott

Reputation: 70183

The syntax for ES6 export looks like this:

//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}

//------ main.js ------
import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5

Note that this is different from the CommonJS modules.export syntax used in Node.js.

Upvotes: 5

Related Questions