【问题标题】:Uncaught ReferenceError: exports is not defined //year 2020未捕获的 ReferenceError:未定义出口 //2020 年
【发布时间】:2020-08-18 16:17:16
【问题描述】:

我被困住了。我尝试了许多解决方案,但没有一个有效。 尝试实现一个模块,我收到以下错误消息:

未捕获的引用错误:未定义导出

我试过solution from Stack Overflow,但它不起作用。更改 tsconfing 中的任何内容都没有任何区别。我在 HTML 文件的脚本中添加了var exports = {};,错误从“exports”更改为“require”——死路一条。

服务器节点:

var fs = require('fs');
const express = require('express');
var path = require('path');
var { request } = require('http');
var app = express();
app.use(express.static(path.join(__dirname, 'public')));
app.listen(3533);

html:

   <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="description" content="testing site">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" type="text/css" href="style.css">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>simple testin </title>
</head>
<body >
</body>
<script type="text/javascript" src="webscripts/run_scripts.js"></script>
<script type="text/javascript" src="webscripts/library.js"></script>  
</html>

库.ts:

export function appearSomeTestingSentence()  {
    document.write('111111');
};

库.js:

  "use strict";
exports.__esModule = true;
exports.appearSomeTestingSentence = void 0;
exports.appearSomeTestingSentence = function () {
    document.write('111111');
};

run_scripts.ts:

import {appearSomeTestingSentence} from './library.js';
appearSomeTestingSentence();

run_scripts.js:

"use strict";
exports.__esModule = true;
var library_1 = require("./library");
library_1.appearSomeTestingSentence();

【问题讨论】:

  • 您声称无效的链接问题有 17 个答案。您尝试了哪个答案?您是如何实施解决方案的?他们是怎么不工作的?
  • 我尝试在我的 html 文件中添加评论 ' "module": "commonjs" ' 我把: ' '。它将错误代码更改为“require”而不是“imports”。我试着改变 tsconfig 像:' { "compilerOptions": { "module": "es6", "target": "es5", } }' 没有任何效果!
  • &lt;script type=module&gt; 不适用于 CommonJS。
  • 我已经改了 Aluan Haddad,没区别

标签: javascript typescript tsc


【解决方案1】:

最后我把它整理出来。在使用 tsc 编译之前,我必须删除 *.js 文件,并且在更改 tsconfig 中的某些内容后必须重新启动 tsc。问题解决后 在 tsconfig 文件中将模块更改为"module" : "ESNext"

【讨论】:

    猜你喜欢
    • 2013-12-06
    • 2023-01-23
    • 2016-01-05
    • 2016-11-03
    • 2011-01-05
    • 2016-01-02
    • 2013-10-06
    • 2016-12-17
    相关资源
    最近更新 更多