gpt4 book ai didi

reactjs - 如何修复 "SyntaxError: Cannot use import statement outside a module"

转载 作者:行者123 更新时间:2023-12-04 13:54:07 25 4
gpt4 key购买 nike

我正在尝试将我的第一个 React Typescript JSX 组件 npm 包放在一起。
我将 CRA TypeScript 项目中的工作代码复制到一个空文件夹中,并添加了以下 package.jsontsconfig.json .
包.json

{
"name": "react-hashlink",
"version": "0.0.1",
"description": "React hash link",
"main": "lib/HashLink.js",
"keywords": [
"react",
"hash",
"link"
],
"peerDependencies": {
"react": "^17.0.0",
"react-dom": "^17.0.0",
"react-router-dom": "^5.0.0"
},
"dependencies": {},
"devDependencies": {
"@types/react": "^17.0.0",
"@types/react-router-dom": "^5.1.7",
"@types/styled-components": "^5.1.7",
"npm-check-updates": "^10.2.5",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-router-dom": "^5.2.0",
"typescript": "^4.1.3"
},
"scripts": {
"code": "tsc -w",
"build": "rm -fr lib/*; tsc",
"ncu": "ncu -u"
},
"prettier": {
"endOfLine": "lf",
"printWidth": 80,
"semi": false,
"tabWidth": 2,
"trailingComma": "es5"
}
}
配置文件
{
"compilerOptions": {
"allowJs": true,
"allowSyntheticDefaultImports": true,
"declaration": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"isolatedModules": true,
"jsx": "react-jsx",
"lib": ["dom", "dom.iterable", "esnext"],
"module": "esnext",
"moduleResolution": "node",
"noFallthroughCasesInSwitch": true,
"outDir": "lib",
"resolveJsonModule": true,
"skipLibCheck": true,
"sourceMap": true,
"strict": true,
"target": "es5"
},
"include": ["src"]
}
当我导入 react-hashlink使用 npm linknpm link react-hashlink在一个项目中,抛出以下错误。

SyntaxError: Cannot use import statement outside a module


我可能错过了一些东西。想法?

最佳答案

使用 "module": "commonjs""target": "esnext"做的工作!

关于reactjs - 如何修复 "SyntaxError: Cannot use import statement outside a module",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65760809/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com