TypeScript错误:构建时出现ReferenceError: exports is not defined
大家好!我是TypeScript新手,在尝试编译和运行项目时遇到了问题。我使用的是Node.js(版本18)和TypeScript(版本5.0)。这是我的简单代码:
```typescript
// math.ts
export function add(a: number, b: number): number {
return a + b;
}
export const PI = 3.14;
```
```typescript
// main.ts
import { add, PI } from './math';
console.log(add(2, 3));
console.log(PI);
```
我用命令 `tsc main.ts` 编译了代码,得到了 `math.js` 和 `main.js` 文件。当我运行 `node main.js` 时,出现了错误:
```
ReferenceError: exports is not defined
at Object. (/path/to/main.js:2:23)
```
在生成的JS文件中,我看到了 `exports.add = ...` 和 `exports.PI = ...` 的使用。我读到这与模块有关,但我不明白具体哪里做错了。我的 `tsconfig.json` 是这样的:
```json
{
"compilerOptions": {
"target": "ES6",
"module": "commonjs",
"outD