简体   繁体   English

如何修复“语法错误:无法在模块外使用导入语句”

[英]How to fix “SyntaxError: Cannot use import statement outside a module”

I am trying to put together my first React Typescript JSX component npm package.我正在尝试将我的第一个 React Typescript JSX 组件 npm package 放在一起。

I copied working code from a CRA TypeScript project to an empty folder and added the following package.json and tsconfig.json . I copied working code from a CRA TypeScript project to an empty folder and added the following package.json and tsconfig.json .

package.json package.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"
  }
}

tsconfig.json tsconfig.json

{
  "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"]
}

When I import react-hashlink using npm link and npm link react-hashlink in a project, the following error is thrown.当我在项目中使用npm linknpm link react-hashlink导入react-hashlink时,抛出以下错误。

SyntaxError: Cannot use import statement outside a module SyntaxError:不能在模块外使用 import 语句

I am likely missing something.我可能错过了一些东西。 Ideas?想法?

Using "module": "commonjs" and "target": "esnext" did the job!使用"module": "commonjs""target": "esnext"完成了这项工作!

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

相关问题 如何修复 SyntaxError:无法在使用 Jest 的模块外部使用 import 语句? - How do I fix SyntaxError: Cannot use import statement outside a module using Jest? 'SyntaxError: 不能在模块外使用 import 语句' - 'SyntaxError: Cannot use import statement outside a module' SyntaxError: 不能在模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module SyntaxError: 无法在动态导入 Nextjs 的模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module with dynamic import of Nextjs 导入 React = 'SyntaxError: 不能在模块外使用导入语句' - Importing React = 'SyntaxError: Cannot use import statement outside a module' React 16: SyntaxError: 不能在模块外使用 import 语句 - React 16: SyntaxError: Cannot use import statement outside a module SyntaxError: 不能在模块外使用 import 语句——React - SyntaxError: Cannot use import statement outside a module — React SyntaxError:无法在 javascript 中的模块外使用 import 语句 - SyntaxError: Cannot use import statement outside a module in javascript 未捕获的 SyntaxError 无法在模块外使用 import 语句 - Uncaught SyntaxError Cannot use import statement outside a module SyntaxError:无法在模块 React JS Antd 之外使用 import 语句 - SyntaxError: Cannot use import statement outside a module React JS Antd
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM