-2

I am writing some Javascript code. I created a js file which contain my exports. However, when I import it into the other js file, I get an error which says SyntaxError: Cannot use import statement outside a module.

I am using WebStorm. Here is the code:

exports(math.js).

export var add=function (n1,n2) { return n1 + n2; };

export var subtract=function (n1,n2) { return n1 - n2; };

export var multiply=function (n1,n2) { return n1 * n2; };

export var divide=function (n1,n2) { return n1 / n2; };


import import * as math from './math';

console.log(math.add(2,2)); console.log(math.subtract(2,2)); console.log(math.multiply(2,2)); console.log(math.divide(2,2));
4
  • Have you tried copy/pasting the error message into Google? Commented Mar 24, 2020 at 15:19
  • yes I did but could not find a satisfying answer. Commented Mar 24, 2020 at 15:21
  • Add the code you wrote to the post Commented Mar 24, 2020 at 15:22
  • @MajedBadawi — What would that tell us that the error message doesn't? Commented Mar 24, 2020 at 15:22

1 Answer 1

0

use require instead of import

const someImported =require('fileName.js')

Sign up to request clarification or add additional context in comments.

3 Comments

That would only work if the environment supports require
Sorry required did not work.
can you share the code in order to know the context

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.