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

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"
    ]
}

当前回答

所以这是一个超级通用的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(只引用目录中导出的文件)

其他回答

编辑:

这个答案可能不起作用,这取决于您是否不再针对es5,我将尝试使答案更完整。

原来的答案

如果CommonJS没有安装(它定义了exports),你必须从tsconfig.json中删除这一行:

 "module": "commonjs",

根据注释,仅这一点可能不适用于tsc的后续版本。如果是这样的话,你可以安装一个模块加载器,比如CommonJS, SystemJS或RequireJS,然后指定它。

注意:

看看tsc生成的main.js文件。你会发现在最上面:

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

它是错误信息的根,删除"module": "commonjs",,它将消失。

我有同样的问题,并解决了它添加“es5”库tsconfig。Json是这样的:

{
    "compilerOptions": {
        "target": "es5", //defines what sort of code ts generates, es5 because it's what most browsers currently UNDERSTANDS.
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true, //for angular to be able to use metadata we specify in our components.
        "experimentalDecorators": true, //angular needs decorators like @Component, @Injectable, etc.
        "removeComments": false,
        "noImplicitAny": false,
        "lib": [
            "es2016",
            "dom",
            "es5"
        ]
    }
}

首先,在你的公共文件夹中找到你的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'

对于一些ASP。NET项目的导入和导出可能根本不会在Typescripts中使用。

当我试图这样做时,问题的错误出现了,我后来才发现,我只需要像这样将生成的JS脚本添加到视图:

<script src="~/scripts/js/[GENERATED_FILE].Index.js" asp-append-version="true"></script>

我遇到了同样的问题,但我的设置需要不同的解决方案。

我使用的是带有config-override .js文件的create-react-app-rewired包。以前,我从customize-cra中使用addBabelPresets导入(在override()方法中),并决定将这些预置抽象到一个单独的文件。巧合的是,这解决了我的问题。

我在config-override .js的override()方法中添加了useBabelRc(),并创建了一个babel.config.js文件,内容如下:

module.exports = {
    presets: [
        '@babel/preset-react',
        '@babel/preset-env'
    ],
}