typescript 错误 cannot use import statement outside a module-ag捕鱼王app官网

当前位置:ag捕鱼王app官网 > > 编程语言 > >

typescript 错误 cannot use import statement outside a module

作者:迹忆客 最近更新:2022/10/31 浏览次数:

要解决 typescript 中的“cannot use import statement outside a module”错误,需要在 tsconfig.json 文件中将 module 选项设置为 commonjs,并确保编译 typescript 文件(例如使用 ts-node),而不是使用 node 直接运行它们。

{
  "compileroptions": {
    "target": "es6",
    "module": "commonjs",
    "esmoduleinterop": true,
    // ... 其他选项
  }
}

module 选项设置程序的模块系统。

esmoduleinterop 选项默认设置为 false,这使其将 commonjs 模块视为类似于 es6 模块。 这会导致一些问题。 将 esmoduleinterop 设置为 true 可以解决这些问题。

typescript 中“cannot use import statement outside a module”错误的另一个常见原因是尝试直接使用 node 运行 typescript 文件,例如 node src/index.ts。 这不起作用,因为我们首先必须将文件转换为 javascript,然后才能使用 node.js 运行它。

如果我们错误地配置了使用 babel 或 ts-node 的 typescript 项目,情况也是如此。

我们的 typescript 文件是否被转译为构建目录中的 javascript 文件? 构建目录应该只包含 javascript 文件。 如果它包含 typescript 文件,则我们项目配置不正确。

如果我们的项目使用 ts-node,则可以尝试在 tsconfig.json 中添加覆盖,将模块设置为 commonjs。

{
  "ts-node": {
    // 这些选项是仅由 ts-node 使用的覆盖
    "compileroptions": {
      "module": "commonjs"
    }
  },
  "compileroptions": {
    // ... 我们的配置项
  },
}

我们应该检查的另一件事是 package.json 文件中的 main 属性指向我们的 index.js 文件,而不是 index.ts 文件。

查看我们通过 tsconfig.json 文件中的 outdir 选项设置的build文件夹。 我们配置可能有误,仍然可以在build目录中生成 typescript 文件。

如果上述建议都不起作用,请查看使用 ts-node 的 typescript 项目的工作配置。

这是一个较完整的 tsconfig.json 文件。

{
  "compileroptions": {
    "skiplibcheck": true,
    "target": "es6",
    "module": "commonjs",
    "moduleresolution": "node",
    "allowjs": true,
    "resolvejsonmodule": true,
    "esmoduleinterop": true,
    "outdir": "./build",
    "rootdir": "src",
  },
  "include": ["src/**/*"],
  "exclude": ["node_modules"]
}

确保包含数组指向包含项目所有必要文件的目录。

这是一个 package.json 文件。

{
  "name": "example",
  "version": "1.0.0",
  "main": "build/index.js",
  "scripts": {
    "build": "rimraf ./build && tsc",
    "dev": "nodemon",
    "start": "npm run build && node build/index.js",
  },
  "devdependencies": {
    "@types/node": "^17.0.21",
    "nodemon": "^2.0.15",
    "rimraf": "^3.0.0",
    "ts-node": "^10.4.0",
    "typescript": "^4.6.2"
  }
}

这里是 nodemon.json 配置文件,它位于同一目录中。

{
  "watch": ["src"],
  "ext": ".ts,.js",
  "ignore": [],
  "exec": "ts-node --files ./src/index.ts"
}

此项目结构假定我们将所有 typescript 文件放在一个名为 src 的目录中,并且在 src/index.ts 下有一个 index.ts 入口点。

ts-node 包会将我们的代码转换为 javascript 并使用 node.js 运行它。

我们的构建文件将位于 build 目录中。 请注意,我们的 build 目录不应包含任何 typescript 文件,而应仅包含 javascript 文件。

如果我们的 build 目录包含 typescript 文件,则项目中存在配置错误。

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

发布时间:2023/03/19 浏览次数:254 分类:

本教程指南通过特定的实现和编码示例深入了解了 typescript 中 declare 关键字的用途。

发布时间:2023/03/19 浏览次数:962 分类:

本篇文章演示了类的 get 和 set 属性以及如何在 typescript 中实现它。

在 typescript 中格式化日期和时间

发布时间:2023/03/19 浏览次数:269 分类:

本教程介绍内置对象 date() 并讨论在 typescript 中获取、设置和格式化日期和时间的各种方法。

在 typescript 中返回一个 promise

发布时间:2023/03/19 浏览次数:586 分类:

本教程讨论如何在 typescript 中返回正确的 promise。这将提供 typescript 中 returns promise 的完整编码示例,并完整演示每个步骤。

在 typescript 中定义函数回调的类型

发布时间:2023/03/19 浏览次数:1445 分类:

本教程说明了在 typescript 中为函数回调定义类型的ag捕鱼王app官网的解决方案。为了程序员的方便和方便,实施了不同的编码实践指南。

使用 npm 将 typescript 更新到最新版本

发布时间:2023/03/19 浏览次数:446 分类:

本教程说明了如何使用 npm 更新到最新版本的 typescript。这将为如何使用 npm 将 typescript 更新到最新版本提供完整的实际示例。

使用 jquery 和 typescript

发布时间:2023/03/19 浏览次数:246 分类:

本教程提供了使用 jquery 和 typescript 的基本理解和概念。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便
网站地图