Get "SyntaxError: Cannot use import statement outside a module" when attempting to import a class in Jasmine spec file

Viewed 2917

As an exercise I decided to create a little vanilla JavaScript game using ES6 syntax that runs in the browser. The program works fine.

I'd like to test it using Jasmine. However, whenever I try to perform an import e.g.

import Deck from "../Deck.js";

Deck.js starts:

export default class Deck {

I get error SyntaxError: Cannot use import statement outside a module.

Things I've done:

  • Installed node v13.0.1 - I thought this version of node allowed es6 modules.
  • Installed jasmine and initialized node ./node_module/jasmine/bin/jasmine init
  • Run node ./node_module/jasmine/bin/jasmine - works fine without imports
  • Run node --experimental-modules ./node_module/jasmine/bin/jasmine - doesn't work with imports
  • Tried require instead of import: const Deck = require('../Deck.js'); - SyntaxError: Unexpected token 'export'

How do I get jasmine to work with imports? At the moment I cannot include any files to test !

I'm sure I've gone about this the wrong way, but i just want some cmd line tests.

1 Answers
Related