[英]Error Typescript - Uncaught ReferenceError: exports is not defined
我正面临这个问题。 我创建了这个简单的 app.ts 来了解 Typescript 中的承诺:
import {Promise} from 'es6-promise'
(() => {
console.log('inicio');
const prom1 = new Promise((resolve, reject) => {
setTimeout(() => {
resolve('Se termino el timeout');
}, 1000);
});
prom1
.then(mensaje => console.log(mensaje))
.catch(err => console.warn(err))
console.log('fin');
})();
然后我编译这个(tsc app.ts)。 它创建了一个 app.js:
"use strict";
exports.__esModule = true;
var es6_promise_1 = require("es6-promise");
(function () {
console.log('inicio');
var prom1 = new es6_promise_1.Promise(function (resolve, reject) {
setTimeout(function () {
resolve('Se termino el timeout');
}, 1000);
});
prom1
.then(function (mensaje) { return console.log(mensaje); })["catch"](function (err) { return console.warn(err); });
console.log('fin');
})();
此外,这是 tsconfig.json
{
"exclude": ["typescript"],
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"noImplicitAny": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
}
}
但是当我在浏览器控制台中运行它时,会出现以下警报:
Uncaught ReferenceError: exports is not defined
at app.js:2
我很沮丧,尝试了互联网上出现的所有内容,但没有任何效果。 我正在尝试学习如何使用 Angular
但是当我在控制台中运行它时
如果您的意思是浏览器控制台,那是预期的。 该代码不是为在浏览器中独立使用而编译的,而是为理解 CommonJS 模块的东西而编译的。 (浏览器不支持,没有库/捆绑器支持。)
If you just want to compile TypeScript to JavaScript and run the result in a browser directly, tell TypeScript to output ESM (via the module
option, setting it to "ESNext"
).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.