[英]Stackblitz: Cannot use import statement outside a module
Using stackblitz even if I create simplest demo for this library I get this error:使用 stackblitz 即使我为这个库创建了最简单的演示,我也会收到这个错误:
Cannot use import statement outside a module
不能在模块外使用 import 语句
I found related question from here, but it doesn't help:我从这里找到了相关的问题,但它没有帮助:
package.json
because I think they suggest to edit package.json
of module which I importedpackage.json
因为我认为他们建议编辑package.json
导入的模块index.html
fileindex.html
文件import React from 'react';
import { FaBeer } from 'react-icons/fa';
class Question extends React.Component {
render() {
return (
<h3>
{' '}
Lets go for a <FaBeer />?{' '}
</h3>
);
}
}
Can someone help?有人可以帮忙吗?
You need to configure your local tsconfig like in this example您需要像此 示例中一样配置本地 tsconfig
{
"compilerOptions": {
"module": "esnext",
"noImplicitAny": true,
"removeComments": true,
"preserveConstEnums": true,
"esModuleInterop": true,
"sourceMap": true,
"lib": ["es6", "dom", "es2017"]
}
}
in this approach, we are just specifying esnext module types instead of commonjs, and turning on esModuleInterop (needed for react itself)在这种方法中,我们只是指定 esnext 模块类型而不是 commonjs,并打开 esModuleInterop(需要自己做出反应)
I do notice that with some frequency when reopening this example in firefox, often I get an error:我确实注意到,在 firefox 中重新打开此示例时,经常会出现错误:
Error in /~/question.tsx (24:21)
import declarations may only appear at top level of a module
but opening/reopening in chrome has not produced this issue.但在 chrome 中打开/重新打开并没有产生这个问题。 Simply modifying the tsconfig.json file in any way, saving, then undoing and saving again causes it to then load correctly in firefox.
只需以任何方式修改 tsconfig.json 文件,保存,然后撤消并再次保存,即可使其在 firefox 中正确加载。 This leads me to believe that @StackBlitz has a configuration issue related to some non-chrome browsers, it is not an issue with the code.
这让我相信@StackBlitz 存在与某些非 Chrome 浏览器相关的配置问题,这不是代码问题。
Inside package.json add内部 package.json 添加
{
"type":"module"
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.