Mocha: SyntaxError: Cannot use import statement outside a module

Created on 9 Dec 2020  路  2Comments  路  Source: mochajs/mocha

Current behavior
I faced intricate issue, posted on SOF before: https://stackoverflow.com/questions/65164157/set-up-testing-library-react-with-mocha-got-syntaxerror-unexpected-token

Please clone this repo: https://github.com/inovramadani/babel-bug
And then run npm test
Should get this error:

import Button from './button';
^^^^^^

SyntaxError: Cannot use import statement outside a module

Expected behavior
Should be able to run the test smoothly and not getting any error.

Environment
Tried on 3 different OS-es and 3 different npm and node versions, got same error. So I think has nothing to do with the environment.

question

All 2 comments

I think you have to use require for importing

const Button = require('./button');

I found this about compiling ES6 import statement
https://stackoverflow.com/questions/59042716/trying-es6-style-import-gives-cannot-use-import-statement-outside-a-module

Was this page helpful?
0 / 5 - 0 ratings