我对打字稿还比较陌生,我完全不明白为什么在执行tsc Program.ts
之后再运行
Program.js
在浏览器中对我大喊ReferenceError: exports is not defined
Program.ts
import { TestClass } from './TestClass';
var tc = new TestClass();
console.log(tc.Test("test"));
TestClass.ts
import {Data} from "./Data";
export class TestClass
{
constructor() {}
Test(originalString:string)
{
console.log(Data.Symbols["John"] + " " + originalString);
}
}
Data.ts
export abstract class Data
{
public static Symbols:{[symbol: string] : string} =
{
"John":"a5fq36831v",
}
}
package-lock.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.json
或babelrc
文件。
此技巧部分对我有效<script>var exports = {};</script>
t
因为之后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
答案 0 :(得分:0)
您需要检查commonjs是否安装在环境中。
答案 1 :(得分:0)
您只需在项目的根目录中添加一个tsconfig.json
文件,即可指定编译目标。这样的事情很可能会起作用:
{
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"sourceMap": true,
"lib": [
"dom",
"es2015",
"es5",
"es6"
]
}
}
答案 2 :(得分:0)