gpt4 book ai didi

javascript - 导出未定义为 ia tsc'ed (js) 文件

转载 作者:搜寻专家 更新时间:2023-10-30 21:13:24 25 4
gpt4 key购买 nike

我对 typescript 比较陌生,我很不明白为什么在执行 tsc Program.ts 之后然后运行

Program.js在浏览器中它对我大喊大叫 ReferenceError: exports is not defined

程序.ts

import { TestClass } from './TestClass';

var tc = new TestClass();

console.log(tc.Test("test"));

测试类.ts

import {Data} from "./Data";

export class TestClass
{
constructor() {}

Test(originalString:string)
{
console.log(Data.Symbols["John"] + " " + originalString);
}
}

数据.ts

export abstract class Data
{
public static Symbols:{[symbol: string] : string} =
{
"John":"a5fq36831v",
}
}

包锁.json

{
"requires": true,
"lockfileVersion": 1,
"dependencies":
{
"uniq":
{
"version": "1.0.1",
"resolved": "https://registry.npmjs.org/uniq/-/uniq-1.0.1.tgz",
"integrity": "sha1-sxxa6CVIRKOoKBVBzisEuGWnNP8="
}
}
}

我有 npm i @types/node安装

我看过这个帖子:Typescript ReferenceError: exports is not defined

但我没有tsconfig.jsonbabelrc文件。

这个技巧部分对我有用 <script>var exports = {};</script>

因为之后ReferenceError: require is not defined

是否可以在不安装额外大量软件的情况下使其工作?

npm i common-js

npm WARN saveError ENOENT: no such file or directory, open 'C:\Repo\Master\src\TypeScript\package.json'
npm WARN saveError EPERM: operation not permitted, rename 'C:\Repo\Master\src\TypeScript\package-lock.json.1161578677' -> 'C:\Repo\src\TypeScript\package-lock.json'
npm WARN enoent ENOENT: no such file or directory, open 'C:\Repo\Master\src\TypeScript\package.json'
npm WARN TypeScript No description
npm WARN TypeScript No repository field.
npm WARN TypeScript No README data
npm WARN TypeScript No license field.

+ common-js@0.3.8
added 1 package in 2.141s
PS C:\Repo\Master\src\TypeScript> tsc ProgramParser.ts

ReferenceError: exports is not defined Program.js:2:1
ReferenceError: Parser is not defined html.html:40:6

最佳答案

需要检查环境中是否安装了commonjs。

关于javascript - 导出未定义为 ia tsc'ed (js) 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55360279/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com