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

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

当前回答

我认为问题可能是配置不匹配。

下面的工作解决方案1为您提供了ES模块的正确配置。 下面的工作解决方案2为您提供了正确的CommonJS配置。 混合解决方案1+2给你一个错误。

为了清晰起见,我在这里只发布了部分内容。 Github项目https://github.com/jmmvkr/ts-express/ 准备一套完整的文件来演示工作解决方案1和解决方案2。

工作方案1,ES模块

/* Configuration for ES Module */

// tsconfig.json
{
    "compilerOptions": {
        "module": "es6", // or "esnext"
    }
}
// package.json
{
    "type": "module", // type is module
}

工作解决方案2,CommonJS

/* Configuration for CommonJS */

// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
    }
}
// package.json
{
    "type": "", // type is NOT module
}

混合,不工作

/* Mixed, got ReferenceError: exports is not defined in ES module scope */

// tsconfig.json
{
    "compilerOptions": {
        "module": "commonjs",
    }
}
// package.json
{
    "type": "module", // type is module
}

其他回答

试试@iFreilicht上面建议的方法。如果在你安装了webpack之后没有工作,你可能只是从网上的某个地方复制了一个webpack配置,并在那里配置了你想要输出支持CommonJS的错误。确保在webpack.config.js中不是这样:

module.exports = {
  mode: process.env.NODE_ENV || "development",
  entry: { 
    index: "./src/js/index.ts"
  },
  ...
  ...
  output: {
    libraryTarget: 'commonjs',         <==== DELETE THIS LINE
    path: path.join(__dirname, 'build'),
    filename: "[name].bundle.js"
  }
};

有同样的问题,并通过改变JS包的加载顺序来修复它。

检查调用所需包的顺序,并按适当的顺序加载它们。

在我的特定情况下(不使用模块绑定器),我需要加载Redux,然后Redux坦克,然后React Redux。在Redux坦克之前加载React Redux会给我出口是没有定义的。

注意:这可能不适用于OP的答案,但我得到了这个错误,这就是我解决它的方式。

所以我面临的问题是,当我从一个特定的CDN检索一个'js'库时,我得到了这个错误。

我做的唯一错误的事情是从CDN的cjs目录导入,就像这样: https://cdn.jsdelivr.net/npm/@popperjs/core@2.4.0/dist/cjs/popper.min.js

注意到dist/cjs部分了吗?这就是问题所在。

在我的例子中,我回到CDN (jsdelivr)并导航到umd文件夹。我可以找到另一组popper.min.js,这是要导入的正确文件: https://cdn.jsdelivr.net/npm/@popperjs core@2.4.0 / dist / / popper.min.js umd格式。

我有同样的问题,并解决了它添加“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"
        ]
    }
}

编辑:

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

原来的答案

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

 "module": "commonjs",

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

注意:

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

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

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