简体   繁体   English

无法导入文件,出现 eslint 错误:“import”和“export”可能只出现在“sourceType:module”中

[英]Cant import files, get eslint error: 'import' and 'export' may appear only with 'sourceType: module'

I am using cypress, and I want to import a function from another file.我正在使用柏树,我想从另一个文件导入 function。

For some reason I cant seem to be abe to import things.出于某种原因,我似乎无法导入东西。

I get the error on vscode:我在 vscode 上收到错误消息:

Parsing error: 'import' and 'export' may appear only with 'sourceType: module'eslint

And when i run cypress i getthis error from the line:当我运行 cypress 时,我从以下行得到这个错误:

import test from '../support/helperFunctions'


helperFunctions_1.default is not a function

I think there is no webpack on the project, I searched我认为项目上没有webpack,我搜索了

Could you please tell me in which direction should i look in order to fix this?你能告诉我为了解决这个问题我应该朝哪个方向看吗?

Thanks.谢谢。

This is not a cypress issue.这不是柏树问题。 So import & export of modules are introduced with EcmaScript and by default it doesn't get supported in your JS projects.因此,EcmaScript 引入了模块的导入和导出,默认情况下,您的 JS 项目不支持它。

So in order to get this work, you have to install babel, which is a transpiler(translate & compiler) and configure accordingly.因此,为了完成这项工作,您必须安装 babel,它是一个转译器(翻译和编译器)并进行相应的配置。

Please try to add below two dependencies in your package.json file请尝试在 package.json 文件中添加以下两个依赖项

"@babel/plugin-transform-modules-commonjs": "7.8.3",
"babel-core": "6.26.3",

Now create.babelrc file and add the below statement which will treat modules to be imported and exported.现在创建.babelrc 文件并添加以下语句,它将处理要导入和导出的模块。

{
  "plugins": ["@babel/plugin-transform-modules-commonjs"]
}

Now run your code and verify.现在运行您的代码并验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 “导入”和“导出”可能仅与“ sourceType:模块”一起出现 - 'import' and 'export' may appear only with 'sourceType: module' Vueify:'import'和'export'可能只出现'sourceType:module' - Vueify: 'import' and 'export' may appear only with 'sourceType: module' Keystone + React:“导入”和“导出”可能仅与“ sourceType:模块”一起出现 - Keystone+React: 'import' and 'export' may appear only with 'sourceType: module' Browserify - ParseError: 'import' 和 'export' 可能只与 'sourceType: 模块一起出现 - Browserify - ParseError: 'import' and 'export' may appear only with 'sourceType: module Vue“ ParseError:“导入”和“导出”可能仅与“ sourceType:模块”一起出现” - Vue “ParseError: 'import' and 'export' may appear only with 'sourceType: module'” SyntaxError: 'import' 和 'export' 可能只与 'sourceType: module' 一起出现 - Gulp - SyntaxError: 'import' and 'export' may appear only with 'sourceType: module' - Gulp ParseError: 'import' 和 'export' 可能只出现在 'sourceType: module', browserify - ParseError: 'import' and 'export' may appear only with 'sourceType: module', browserify 开玩笑的返回错误:SyntaxError:“导入”和“导出”可能仅与“ sourceType:“模块””一起出现(21:0) - jest return ERROR: SyntaxError: 'import' and 'export' may appear only with 'sourceType: “module”' (21:0) VSCode ESLint 如何修复解析错误:'import' 和 'export' 可能只出现在顶层 - VSCode ESLint howto fix parsing error: 'import' and 'export' may only appear at the top level 语法错误:导入声明只能出现在模块的顶层。 使用 express,没有 webpack - SyntaxError: import declarations may only appear at top level of a module. Using express, no webpack
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM