Typescript ReferenceError:未定义导出

Geo*_* C. 76 module typescript

尝试按照官方手册实现模块,我收到以下错误消息:

未捕获的ReferenceError:未定义导出

在app.js:2

但我的代码中没有任何地方使用该名称exports.

我怎样才能解决这个问题?


app.ts

let a = 2;
let b:number = 3;

import Person = require ('./mods/module-1');
Run Code Online (Sandbox Code Playgroud)

模块1.T

 export class Person {
  constructor(){
    console.log('Person Class');
  }
}
export default Person;
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
   "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "scripts/"
    },
    "exclude": [
        "node_modules"
    ]
}
Run Code Online (Sandbox Code Playgroud)

Mas*_*iri 37

我通过"type": "module"package.json.

  • 如果没有这个,就不能使用“导入”。 (86认同)
  • 但这为什么有帮助呢? (8认同)
  • 这里有一个很好的解释:https://bobbyhadz.com/blog/typescript-uncaught-referenceerror-exports-is-not-define#nodejs---referenceerror-exports-is-not-definition 在 NodeJS 中,您将指示 TypeScript 生成编译时使用 CommonJS 语法,但使用 "type": "module" 指示 NodeJS 使用 ES6 模块(已编译掉)。 (2认同)

Ven*_*ndi 34

这个问题很少有其他解决方案

  • 在对Javascript的其他引用之前添加以下行.这是一个不错的小黑客.
   <script>var exports = {};</script>
Run Code Online (Sandbox Code Playgroud)
  • 使用最新版本的TypeScript会出现此问题,可以通过引用typescript版本2.1.6来消除此错误

  • 现在出现了错误:`require is not defined` (20认同)
  • 我讨厌这行得通!这绝对有效。没有办法让 TypeScript 转换成 Node 期望的东西吗?Node 也在抱怨吗?或者是浏览器的原因?我无法忍受不理解问题的实质。 (14认同)
  • @ChuckLeButt希望这有助于/sf/ask/1334170631/ (3认同)

iFr*_*cht 24

编辑:

如果你不再瞄准目标es5,这个答案可能无效,我会尝试让答案更完整.

原始答案

如果未安装CommonJS(定义exports),则必须从以下位置删除此行tsconfig.json:

 "module": "commonjs",
Run Code Online (Sandbox Code Playgroud)

根据评论,仅此一点可能不适用于更高版本的tsc.如果是这种情况,您可以安装CommonJS,SystemJS或RequireJS等模块加载器,然后指定它.

注意:

查看生成的main.js文件tsc.你会在最顶层找到这个:

Object.defineProperty(exports, "__esModule", { value: true });
Run Code Online (Sandbox Code Playgroud)

它是错误消息的根,在删除后"module": "commonjs",,它将消失.

  • 如果安装 CommonJS 会使这个错误消失,那么实际上如何安装 CommonJS?我花了一个小时的大部分时间在谷歌上搜索,我找不到任何关于如何这样做的说明。有人可以_请_解释一下吗? (9认同)
  • 如果您的 tsconfig.json 中的目标为 ES3 或 ES5,那么如果未定义,TypeScript 会自动将您的模块设置为 CommonJS。删除模块是不够的,您需要设置其他内容 - 请参阅 https://www.typescriptlang.org/docs/handbook/compiler-options.html (3认同)
  • 是的,我重新编译了我的.ts文件,并且在注释掉“模块”后仍然存在该错误:“ commonJs”,`:( (2认同)
  • 我有模块:amd,而不是模块:commonjs,而且我在转译的 .js 文件中有这一行。 (2认同)

mha*_*y00 16

几周前我遇到了这个问题,并在短期内使用了上面的导出技巧,但最终找到了另一种对我来说非常有效的方法。

因此,与上面的其他一些回复不同,我实际上希望我正在编译的代码成为一个模块。通过设置"target": "es6", "module": "esnext"然后使用而type="module"不是链接到我编译的JS文件type="text/javascript",我编译的代码不再有该行Object.defineProperty(exports, "__esModule", { value: true });,它仍然是一个模块

我的 tsconfig:

{
  "compilerOptions": {
    "target": "es6",
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "module": "esnext",
    "moduleResolution": "node",
    "lib": ["es2016", "esnext", "dom"],
    "outDir": "build",
    "strict": true,
    "strictNullChecks": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "declaration": true,
  },
  "include": ["src/index.ts"],
  "exclude": ["build"]
}
Run Code Online (Sandbox Code Playgroud)

我在 HTML 标签中链接到我编译的代码head

<script src="../build/index.js" type="module" defer></script>
Run Code Online (Sandbox Code Playgroud)

作为奖励,现在我还可以在type="module"主 HTML 代码下方的单独脚本中导入在该 index.js 文件中导出的任何内容body

 <script type="module">
   import { coolEncodingFunction, coolDecodingFunction } from "../build/index.js";
   /* Do stuff with coolEncodingFunction and coolDecodingFunction */
   /* ... */
</script>
Run Code Online (Sandbox Code Playgroud)


Cod*_*rer 13

This is fixed by setting the module compiler option to es6:

{
  "compilerOptions": {     
    "module": "es6",
    "target": "es5",    
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 如果您使用`nodejs`,您需要将`"moduleResolution": "Node"`添加到`tsconfig.json` (4认同)

Hoc*_*tif 8

我的解决方案是对上面所有内容的总结,并添加了一些小技巧,基本上我将其添加到我的 html 代码中

<script>var exports = {"__esModule": true};</script>
<script src="js/file.js"></script>
Run Code Online (Sandbox Code Playgroud)

这甚至允许您使用import而不是require使用电子或其他东西,并且它适用于打字稿 3.5.1,目标:es3 -> esnext。

  • 这里取得了一些进展,错误刚刚更改为“app.js:3 Uncaught ReferenceError: require is not Defined” (5认同)

小智 7

package.json中,添加"type": "module"

在终端中输入

npx tsc --init
Run Code Online (Sandbox Code Playgroud)

它将创建一个 tsconfig.json。

tsconfig.json中,

  1. 改成"target": "es5""target": "es6"
  2. 注释掉//"module": "commonjs",
  3. 取消注释"moduleResolution": "node",

你应该可以走了。

package.json中,创建构建脚本:

"build": "tsc -p tsconfig.json"
Run Code Online (Sandbox Code Playgroud)

然后,当你想要构建时,你只需编译npm run build

然后执行并且应该很好


rub*_*nsa 6

我遇到了同样的问题并解决了它,将“ es5 ”库添加到 tsconfig.json 中,如下所示:

{
    "compilerOptions": {
        "target": "es5", //defines what sort of code ts generates, es5 because it's what most browsers currently UNDERSTANDS.
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true, //for angular to be able to use metadata we specify in our components.
        "experimentalDecorators": true, //angular needs decorators like @Component, @Injectable, etc.
        "removeComments": false,
        "noImplicitAny": false,
        "lib": [
            "es2016",
            "dom",
            "es5"
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)


Cez*_*sto 5

对于仍然有这个问题的人,如果你的编译器目标设置为 ES6,你需要告诉 babel 跳过模块转换。为此,请将其添加到您的.babelrc文件中

{
  "presets": [ ["env", {"modules": false} ]]
}
Run Code Online (Sandbox Code Playgroud)

  • 这样做我得到以下错误:**使用已删除的 Babel 5 选项:.modules - 在 `plugins` 选项中使用相应的模块转换插件。查看 http://babeljs.io/docs/plugins/#modules** (2认同)

小智 5

npm install @babel/plugin-transform-modules-commonjs

并添加到.babelrc插件解决了我的问题。


小智 5

我也有同样的错误。就我而言,这是因为我们的 TypeScript AngularJS 项目中有一个老式的 import 语句,如下所示:

import { IAttributes, IScope } from "angular";
Run Code Online (Sandbox Code Playgroud)

它被编译成这样的 JavaScript:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
Run Code Online (Sandbox Code Playgroud)

这在过去是需要的,因为我们随后IAttributes在代码中使用了它,否则 TypeScript 将不知道如何处理它。但是在删除 import 语句后,转换IAttributesng.IAttributes那两行 JavaScript 行就消失了 - 错误消息也是如此。


归档时间:

查看次数:

130744 次

最近记录:

6 年,3 月 前