在使用Babel解析JS代码时,如果代码中含有ES6(ECMAScript 6.0)风格的import或export语句,如下图的NodeJS代码:
这时可能会出现错误:'import' and 'export' may appear only with 'sourceType: "module"'。错误原因为:BABEL_PARSER_SOURCETYPE_MODULE_REQUIRED。
出错发生在Babel解析代码时,如:
从官方文档得知:使用Babel的parse方法时,默认的sourceType为script。
改为module即可解决此问题。
特别声明:以上内容(如有图片或视频亦包括在内)为自媒体平台“网易号”用户上传并发布,本平台仅提供信息存储服务。
Notice: The content above (including the pictures and videos if any) is uploaded and posted by a user of NetEase Hao, which is a social media platform and only provides information storage services.