vue.config.js Error [ERR_REQUIRE_ESM]: require() of ES Module ... from ... not supported.如何解决?

请问这配置的问题如何解决???

使用vue-cli-service serve启动项目的时候,报错了:

 ERROR  Error loading vue.config.js:
 ERROR  Error [ERR_REQUIRE_ESM]: require() of ES Module D:\pre-study-cesium\vue.config.js from D:\pre-study-cesium\node_modules\@vue\cli-service\lib\Service.js not supported.
vue.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename vue.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in D:\pre-study-cesium\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

image.png

估计原因是出在package.json中加了一个"type":"module"
image.png

可是,如果不加入这个设置,ifcWatchifcBuild这两个命令又执行不了。
以下是附上rollup.config.js里面的配置(这是根据IFC.js的文档去配置的):
image.png

阅读 6.6k
2 个回答
  1. 后来根据IFCjs/hello-world去重新配置,一步步解决的问题。
  2. .js 改成 .mjs:rollup.config.mjsvue.config.mjsimage.png
  3. rollup.config.mjs的配置如下:image.png
  4. package.json的配置如下:image.png
    不用加什么"type":"module"了。

type 改成 common 就行了,现在有一些包是使用 require() 也就是 commonjs 的方式来引入的,所以就会报错。


本文参与了SegmentFault 思否面试闯关挑战赛,欢迎正在阅读的你也加入。
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题