5

My files are all .mjs extension

I have fizz_buzz.mjs and test/fizz_buzz.spec.mjs

test/fizz_buzz.spec.mjs uses

import { fizzBuzz } from '../fizz_buzz'

I run npm test and I get

> mocha test/**/*.spec.mjs                                                                                                                              

/home/durrantm/Dropbox/90_2019/work/code/js/mocha_chai_bdd_tdd_exercises/node_modules/yargs/yargs.js:1163                                               
      else throw err                                                                                                                                    
           ^                                                                                                                                            

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/durrantm/Dropbox/90_2019/work/code/js/mocha_chai_bdd_tdd_exercises/test/fizz_buzz.spec
.mjs                                                                                                                                                    
    at Object.Module._extensions..mjs (internal/modules/cjs/loader.js:1025:9)                                                                          
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Module.require (internal/modules/cjs/loader.js:848:19)

fizz_buzz.mjs currently just has:

$ cat fizz_buzz.mjs 
export function fizzBuzz() {
  return true
}

fyi package .json uses

"scripts": {
  "test": "mocha test/**/*.spec.mjs"
},

in case that is the issue ?

I also tried $ mocha test/**/*.spec.mjs --experimental-modules at the command line but same error

Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • Consider trying the [v7.0.0-esm1](https://github.com/mochajs/mocha/releases/tag/v7.0.0-esm1) release of `mocha` that was published to npm registry approx one week ago. It's description states _"Enables Mocha to load ECMAScript Modules test files"_ Note: It's a _"experimental"_ release and running `npm show mocha dist-tags.latest` reveals that `7.0.0` remains as the `latest` stable version. – RobC Jan 19 '20 at 12:55

0 Answers0