相关疑难解决方法(0)

Typescript ReferenceError:未定义导出

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

未捕获的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)

module typescript

76
推荐指数
11
解决办法
13万
查看次数

如何禁用 TypeScript Object.defineProperty(exports, "__esModule", { value: true })?

ts 编译器在每个文件中发出这一行:

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

但是我的代码在 Nodejs 上运行,我没有编写 libaray,所以我认为这行代码对我来说是不必要的。我怎样才能禁用它?我的编译器选项是:

{
    "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "allowJs": true,
        "sourceMap": true,
        "outDir": "build",
        "moduleResolution": "Node",
        "lib": ["es6"]
    }
}
Run Code Online (Sandbox Code Playgroud)

例如,编译这个 ts 文件:

function add(a: number, b: number): number {
    return a + b
}

export { add }
Run Code Online (Sandbox Code Playgroud)

我有:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
function add(a, b) {
    return a + b;
}
exports.add = add;
//# sourceMappingURL=App.js.map
Run Code Online (Sandbox Code Playgroud)

如何删除第二行?

node.js typescript ecmascript-6

6
推荐指数
1
解决办法
1766
查看次数

标签 统计

typescript ×2

ecmascript-6 ×1

module ×1

node.js ×1