I want to export function from lib.js file to main.js file. I have
// 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
After I connected main.js file to index.html file, in console I can find:
Uncaught SyntaxError: Unexpected token export lib.js:1
What am I doing wrong? Or how to use "export" and "import" properly?
importandexportare not fully supported in the browser. You usually need a bundler (like Webpack) in order to create a package that has everything in it for the browser.