尝试按照官方手册实现一个模块,我得到这个错误消息:

Uncaught ReferenceError:未定义exports 在app.js: 2

但在我的代码中,我从未使用过名称exports。

我该如何解决这个问题?


文件

app.ts

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

import Person = require ('./mods/module-1');

模块- 1. t

 export class Person {
  constructor(){
    console.log('Person Class');
  }
}
export default Person;

tsconfig.json

{
   "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "scripts/"
    },
    "exclude": [
        "node_modules"
    ]
}

当前回答

我也有这个问题。然后我只是把“module”:“commonjs”改为“module”:“ESNext”,一切都正常工作。 这是tsconfig.json:

{
    "compilerOptions": {
      "module": "ESNext",
      "esModuleInterop": true,
      "target": "ESNext",
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
    },
    "lib": ["ESNext"],
  }

希望它能解决你的问题!

其他回答

我也有这个问题。然后我只是把“module”:“commonjs”改为“module”:“ESNext”,一切都正常工作。 这是tsconfig.json:

{
    "compilerOptions": {
      "module": "ESNext",
      "esModuleInterop": true,
      "target": "ESNext",
      "moduleResolution": "node",
      "sourceMap": true,
      "outDir": "dist",
    },
    "lib": ["ESNext"],
  }

希望它能解决你的问题!

有同样的问题,并通过改变JS包的加载顺序来修复它。

检查调用所需包的顺序,并按适当的顺序加载它们。

在我的特定情况下(不使用模块绑定器),我需要加载Redux,然后Redux坦克,然后React Redux。在Redux坦克之前加载React Redux会给我出口是没有定义的。

我有同样的问题,并解决了它添加“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"
        ]
    }
}

我在SSR客户端部分中遇到了这样的错误,因为它使用了一个用tsconfig构建的库。什么带来使用CommonJS模块解析tsc CLI选项编译器选项:目标=== "ES3"或"ES5" ?CommonJS: ES6。当它使用目标ESNext时。

一个解决方案:

从package.json中删除"type": "module"。