npm test - Must use import to load ES Module - but

2020-07-18 06:16发布

问题:

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