首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何修复"SyntaxError:无法在模块外使用导入语句“

如何修复"SyntaxError:无法在模块外使用导入语句“
EN

Stack Overflow用户
提问于 2021-01-17 12:53:21
回答 1查看 893关注 0票数 5

我试图把我的第一个反应类型记录,JSX组件npm包。

我将CRA TypeScript项目中的工作代码复制到一个空文件夹中,并添加了以下package.jsontsconfig.json

package.json

代码语言:javascript
复制
{
  "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"
  }
}

tsconfig.json

代码语言:javascript
复制
{
  "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-hashlinknpm link react-hashlink导入npm link时,会引发以下错误。

SyntaxError:无法在模块外使用导入语句

我可能漏掉了什么。想法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-17 20:36:36

使用"module": "commonjs""target": "esnext"完成了工作!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65760809

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档