【问题标题】:ReferenceError: Can't find variable: exportsReferenceError:找不到变量:导出
【发布时间】:2019-08-12 13:18:30
【问题描述】:

问题:

为什么会出现以下错误?我是否忘记在我的 html 中包含脚本?

ReferenceError: 找不到变量:exports

从导致它的打字稿生成的 javascript:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/* more code */

额外:

tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "noImplicitAny": true,
    "rootDir": ".",
    "sourceRoot": "../../../",
    "outDir": "../../../js/dist/",
    "sourceMap": false
  },
  "exclude": [
    "node_modules"
  ]
}

requirejs 包含在我的 html 中的 js 文件之前

有类似的问题,但这只是关于打字稿,而不是关于 ember/babel/等。

【问题讨论】:

  • @lonut 感谢您的链接,但它们不是我想要的。
  • 你能告诉我们你的 Gruntfile 吗?您是否在使用 System.js(在您的项目中某处是否有对 system.js 和/或 system.js.config 的引用)?我遇到了同样的问题并设法解决了它。
  • @ACOMIT001 如果没有指定模块,那么我相信默认情况下会选择 commonjs。我正在使用 tsc 编译器,而不是 grunt。我也尝试过指定系统模块,但无法使其正常工作。你的解决方案是什么?
  • 我实际上对 Jasmine 也有类似的问题。创建一个定义了导出的文件可以解决这个问题。

标签: javascript typescript


【解决方案1】:

我无法复制。您的 tsconfig.json 导致 tsc 保释

错误 TS5051:选项 'sourceRoot 只能在提供选项 '--inlineSourceMap' 或选项 '--sourceMap' 时使用。

一旦我删除了sourceRoot 选项,输出中就没有对exports 的引用。


$ ls

my.ts       tsconfig.json

$ cat my.ts

console.log(1)

$ cat tsconfig.json

{
  "compileOnSave": true,
  "compilerOptions": {
    "target": "es5",
    "noImplicitAny": true,
    "rootDir": ".",
    "sourceRoot": "../../../",
    "outDir": "../../../js/dist/",
    "sourceMap": false
  },
  "exclude": [
    "node_modules"
  ]
}

$ tsc --version

Version 3.5.3

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-20
    • 2018-04-24
    • 2017-03-02
    • 2016-10-30
    • 2012-03-18
    • 2015-11-16
    相关资源
    最近更新 更多