11

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.

HGPB
  • 4,346
  • 8
  • 50
  • 86

1 Answers1

0

Follow the offical guide from Babel at https://babeljs.io/setup#installation

Then choose one of the options from this solution: https://stackoverflow.com/a/59399717/673351

Personally, I have renamed my spec files to have the mjs extension becaue I would like ot use the LTS (currently 12) version of node.

Oliver Kocsis
  • 633
  • 1
  • 6
  • 12