npm test - Must use import to load ES Module - but I thought I was? (node 12)

空扰寡人 提交于 2020-02-29 06:41:27

问题


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

来源:https://stackoverflow.com/questions/59803401/npm-test-must-use-import-to-load-es-module-but-i-thought-i-was-node-12

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!