尝试按照官方手册实现一个模块,我得到这个错误消息:

Uncaught ReferenceError:未定义exports 在app.js: 2

但在我的代码中,我从未使用过名称exports。

我该如何解决这个问题?


文件

app.ts

let a = 2;
let b:number = 3;

import Person = require ('./mods/module-1');

模块- 1. t

 export class Person {
  constructor(){
    console.log('Person Class');
  }
}
export default Person;

tsconfig.json

{
   "compilerOptions": {
        "module": "commonjs",
        "target": "es5",
        "noImplicitAny": false,
        "sourceMap": true,
        "outDir": "scripts/"
    },
    "exclude": [
        "node_modules"
    ]
}

当前回答

首先,在你的公共文件夹中找到你的index.html,找到脚本的引用<script src="myScript.js"></script>,并添加type="module"

像这样:

<script type="module" src="myScript.js"></script>

其次,在您的tsconfig中。Json,确保目标设置为es5,模块设置为es2015

 "compilerOptions": {
    "target": "es5", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
    /* Modules */
    "module": "es2015", /* Specify what module code is generated. */
}

注意:确保使用.js扩展名导入,例如import Piece from './classes/ Piece .js'

其他回答

没有直接回答这个问题,但是没有stackoverflow线程提到这种情况,我花了一段时间来调试,所以在这里添加它,以防将来它可以帮助别人。

我有错误“导出未定义”来自导入的NPM模块。改变我的typescript配置没有任何作用,因为我无法控制导入的第三方代码。问题是该包的导入语法无效。例如,我已经导入了“@mui/material/TextField/TextField”,但正确的导入应该是“@mui/material/TextField”。

我也犯了同样的错误。在我的例子中,这是因为我们在我们的TypeScript AngularJS项目中有一个老式的import语句,像这样:

import { IAttributes, IScope } from "angular";

它被编译成这样的JavaScript:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });

这在以前是需要的,因为我们在代码中使用了IAttributes,否则TypeScript不知道该用它做什么。 但是在删除import语句并将IAttributes转换为ng之后。IAttributes这两行JavaScript代码消失了——错误消息也消失了。

所以这是一个超级通用的TypeScript错误,这个StackOverflow问题是我研究我的问题的各种查询的第一个结果。它有38.5万的浏览量。

对于那些使用Angular / TypeScript和Angular库使用ng-packagr时看到通用的“ReferenceError: exports is not defined”的人,你需要定义public-api。每个功能/组件/服务的t,这样你就可以将它包含在索引中。例如在这篇文章的回购中找到的

节点16或18(下周LTS为18) Angular 2+(目前有14个) 打印稿4.6.4-4.8.2

一些简单的例子类似于引用Creating Libraries

ng new workspace --no-create-application
cd workspace
ng generate app home --routing=true --style=scss
ng generate app admin --routing=true --style=scss
ng generate library lib
... # include your library 'lib' into your application 'home'
ng build lib --watch &
ng serve home --open

没有直接解释的是你的索引。Ts和public-api。Ts文件需要在每个特性/组件/服务中。如果你有一个复杂的库,比如下面回购中的这些示例特性A、B和C。回购有以下几点:

src / lib 一个 索引。Ts(只引用。/public-api) 公共api。Ts(只引用目录中导出的文件) 功能b 索引。Ts(只引用。/public-api) 公共api。Ts(只引用目录中导出的文件) feature-c 索引。Ts(只引用。/public-api) 公共api。Ts(只引用目录中导出的文件)

我有类似的问题,原来的海报的问题:

我最好告诉你我犯了什么错误以及我是如何改正的,这可能会帮助到别人。

我有javascript nodejs项目,并将其转换为typescript。

之前的包装。我有“类型”:“模块”当我在JavaScript运行。

当我把它转换成TypeScript项目,并开始转换.ts文件中的文件,并使用以下命令开始运行:

npx tsc test.ts && node test.ts

我会得到上面的错误。

我通过简单地从package.json中删除“type”:“module”来摆脱这个错误

我的tsconfig。Json格式如下:

{
    "compilerOptions": {
        "target": "esnext",
        "lib": ["es6", "es5", "es7", "dom"],
        "allowJs": true,
        "esModuleInterop": true,
        "allowSyntheticDefaultImports": true,
        "strict": true,
        "forceConsistentCasingInFileNames": true,
        "module": "es6",
        "outDir": "./build",
        "moduleResolution": "node",
        "resolveJsonModule": true,
        "skipLibCheck": true
    },
    "exclude": ["node_modules", "build"]

我使用节点14和TypeScript 4.2.4

npm安装@babel/plugin-transform- commonjs

添加到.babelrc插件解决了我的问题。