【问题标题】:Typescript: Cannot use import statement outside a module打字稿:不能在模块外使用导入语句
【发布时间】:2020-02-05 00:01:50
【问题描述】:

我在 node js(07.10.19 的 node.js 的最新版本)应用程序中有一个 .ts 文件,其中导入 node-module 而没有默认导出。我使用这个结构:import { Class } from 'abc'; 当我运行代码时,我有这个错误:Cannot use import statement outside a module

在网络中,我看到了许多解决此问题的方法(对于 .js),但这对我没有帮助,可能是因为我有打字稿文件。这是我的代码:

import { Class } from 'abc';
module.exports = { ...
    execute(a : Class ,args : Array<string>){ ...

这是我的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",

    "strict": true
  }
}

【问题讨论】:

  • 您是在浏览器中运行它吗? import 语句是文件中的第一行吗?
  • 您能发布您的 tsconfig.json 文件吗?在 Typescript 中编译时,您可以确定它生成的模块类型,有效类型可能会根据您使用的环境(浏览器/NodeJS)和其他模块(require vs import)而有所不同。只是为了让您了解这有多复杂,Node 有一些 documentation 关于 importrequire 以及如何使它们一起工作。
  • 如果你使用module.exports语法,你可能不在ES6模块中。
  • Evert,这是 node js 应用,tsconfig 将很快添加
  • 好的,我不能使用require,因为在使用构造execute(a : abc.Class...时找不到命名空间'abc'@

标签: typescript ecmascript-6


【解决方案1】:

Adding "type": "module" to package.json 会告诉 Node 你正在使用 ES2015 模块,这应该可以消除错误,但是你需要通过在 tsconfig 中设置 "module": "es2015" 而不是 "commonjs" 来告诉 Typescript 生成这种类型的模块。 json.

然而,这会导致当前代码出现问题,因为尽管您使用的是 ES6 import {} 语句,但您正在使用 commonJS module.exports = {} 语法导出,并且 Node 的 ES 模块加载器会出现问题。有两种处理方式:

  1. 保留module.exports,但通过giving it a .cjs extension 告诉Node 将此文件解释为commonJS。
  2. 将导出语句更改为 ES2015 语法:export function execute(…)..

第一个选项可能会有点棘手,因为编译器会输出 .js 文件,而您必须一直将其更改为 .cjs(据我所知)。使用第二个选项,您应该能够使用 Node 运行文件(包括版本

如果您绝对需要使用 commonJS,也许最好安装 Node 的类型定义:@types/node 并将导入更改为 commonJS 格式:require('abc') 并保持其余设置不变(尽管您可以在 package.json 中添加 “type”: “commonjs” 来明确)。

【讨论】:

  • require ('abc').Class 不能在 .ts 定义中使用(例如:Class)... 关于 .cjs 扩展名... 类型只能在 .ts 文件中... ES2015 也不是我的选择,因为我在任何地方都使用带有旧导入(需要)的这个模块......我使用 JSDoc 解决了这个问题。不过谢谢回答
  • 对于 webpack 生成的包,我收到错误消息:“ReferenceError: require is not defined in ES module scope, you can use import instead”?如何防止这种情况发生?
【解决方案2】:

确保package.json 中的"main" 字段指向已编译的index.js 而不是index.ts

【讨论】:

    【解决方案3】:

    我有非常相似的问题。我必须安装 nodemon 并始终通过 nodemon index.ts

    启动脚本
    yarn add -D nodemon
    

    package.json

    "scripts": {
       "start": "nodemon index.ts"
    }
    

    或从命令行指定文件

    package.json

    "scripts": {
       "nodemon": "nodemon $1"
    }
    

    【讨论】:

    • 这似乎对我有用(安装了ts-node,使用import 在package.json 中没有`"type": "module" 的语句。
    【解决方案4】:

    获得的可能性有很多,

    SyntaxError: 不能在模块外使用 import 语句

    在运行打字稿时。

    在我的设置中,我从脚本testab.ts 中导入了一个名为AbModuleAbModule 具有类AbClassName)的模块。

    testab.tsimport stmt

    import {AbClassName} = 'testAdapterDir/AbModule.po.ts';
    

    然而, AbModule 拥有所有 *.ts 文件,并且有 *.js 文件不存在。 修复是,

    您在运行以下代码时可能会遇到错误,但您可以放心地忽略它们。

    cd AbModule path
    tsc *.ts
    

    现在AbModule 也应该包含所有*.js 的编译文件。 现在运行testab.ts,发现上面提到的错误已经不存在了,

    【讨论】:

      【解决方案5】:

      安装包 ts-node 并更改你的 package.json

      "scripts": {
          "dev": "ts-node index.ts"
        }
      

      UPD:这可能在使用 ts-node 时发生

      【讨论】:

      • 这可能在使用 ts-node 时发生
      • 你不应该在生产中使用 ts-node。
      猜你喜欢
      • 2021-12-30
      • 2021-09-01
      • 2020-07-14
      • 2021-02-06
      • 2021-07-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      相关资源
      最近更新 更多