Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: Cannot use import statement outside a module #5125

Closed
johnsmithsren opened this issue Jan 17, 2023 · 8 comments
Closed

SyntaxError: Cannot use import statement outside a module #5125

johnsmithsren opened this issue Jan 17, 2023 · 8 comments

Comments

@johnsmithsren
Copy link

Your detail info about the Bug:

出现这样一个报错,[egg-scripts] SyntaxError: Cannot use import statement outside a module

查了很久,网上有人推荐在package.json中修改type:module 来解决,但是尝试了之后发现会导致egg内部其他的报错。暂时未找到其他的解决方法,所以问问有什么解决方案

Reproduction Repo

null

Node Version

16.14.1

Eggjs Version

3.12.0

Plugin Name and its version

null

Platform and its version

win11

@fengmk2
Copy link
Member

fengmk2 commented Jan 17, 2023

egg 目前不支持 esm,请使用 cjs 模式

@fengmk2 fengmk2 closed this as completed Jan 17, 2023
@zldoc88
Copy link

zldoc88 commented Sep 25, 2023

这个是巨坑,依赖第三方模块如果是esm,彻底凉!!!

@fengmk2
Copy link
Member

fengmk2 commented Sep 25, 2023

@zldoc88 这是基础常说,跟 egg 无关。egg 目前只支持 cjs。

@zldoc88
Copy link

zldoc88 commented Sep 25, 2023

@zldoc88 这是基础常说,跟 egg 无关。egg 目前只支持 cjs。

我现在用的场景是 egg - ts, 依赖模块a, a里面依赖b模块,我把a的源码本地重打出个cjs,但b模块是esm模块,但引a进去后彻底崩溃。。。。
这种场景还仅仅是一个例子,egg目前的ts 开发代价非常大,基础要rebuild 第三方模块为cjs。

@fengmk2
Copy link
Member

fengmk2 commented Sep 25, 2023

ts 并不解决 esm 和 cjs 的兼容性问题的,这是两码事。

@atian25
Copy link
Member

atian25 commented Sep 25, 2023

@fengmk2 考不考虑把 egg-core 里面的 FileLoader 从 require 改为 import ? 对应用侧应该 break 不了,只是框架侧有自定义 loader lifecycle 的,如果有覆盖了 loader 内置的函数,需要改为 async + await,改动量很小。

@fengmk2
Copy link
Member

fengmk2 commented Sep 25, 2023

@atian25 已经建了一个长期计划 #5257 全面支持 esm 和 cjs,egg-core 也在使用 ts 重构中,估计要很长一段时间才会发布。

@liuhanqu
Copy link
Contributor

egg-core 会替换成 artusjs/core 么

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants