【问题标题】:TypeScript Error "Cannot read property 'exports' of undefined"TypeScript 错误“无法读取未定义的属性 'exports'”
【发布时间】:2017-01-18 08:39:28
【问题描述】:

我正在尝试使用 TypeScript 构建 Redux 中间件。这个函数

import { Middleware } from "redux"

function createMiddleware() {
  return ({ dispatch, getState }) => next => action => {
    if (Math.random() > 0.5) {
      return next(action)
    }

    return next(action)
  }
}

export const middleware: Middleware = createMiddleware()

产生一个 TypeScript 错误:

Typescript error: Cannot read property 'exports' of undefined

虽然当我删除 if 块时编译器没有抱怨。

我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES5",
    "noImplicitAny": false,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "jsx": "react",
    "lib": ["dom", "es2015", "es2016"],
    "baseUrl": "../src",
    "exclude": [
      "node_modules"
    ]
  }
}

编辑来自浏览器控制台的堆栈跟踪

Uncaught Error: Module build failed: TypeError: Cannot read property 'exports' of undefined
    at resolveName (..\node_modules\typescript\lib\typescript.js:24948:70)
    at getResolvedSymbol (..\node_modules\typescript\lib\typescript.js:32549:67)
    at checkIdentifier (..\node_modules\typescript\lib\typescript.js:33700:26)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38185:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at checkPropertyAccessExpressionOrQualifiedName (..\node_modules\typescript\lib\typescript.js:35587:24)
    at checkPropertyAccessExpression (..\node_modules\typescript\lib\typescript.js:35554:20)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38208:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at resolveCallExpression (..\node_modules\typescript\lib\typescript.js:36556:28)
    at resolveSignature (..\node_modules\typescript\lib\typescript.js:36786:28)
    at getResolvedSignature (..\node_modules\typescript\lib\typescript.js:36809:26)
    at checkCallExpression (..\node_modules\typescript\lib\typescript.js:36836:29)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38213:28)
    at resolveName (..\node_modules\typescript\lib\typescript.js:24948:70)
    at getResolvedSymbol (..\node_modules\typescript\lib\typescript.js:32549:67)
    at checkIdentifier (..\node_modules\typescript\lib\typescript.js:33700:26)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38185:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at checkPropertyAccessExpressionOrQualifiedName (..\node_modules\typescript\lib\typescript.js:35587:24)
    at checkPropertyAccessExpression (..\node_modules\typescript\lib\typescript.js:35554:20)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38208:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at resolveCallExpression (..\node_modules\typescript\lib\typescript.js:36556:28)
    at resolveSignature (..\node_modules\typescript\lib\typescript.js:36786:28)
    at getResolvedSignature (..\node_modules\typescript\lib\typescript.js:36809:26)
    at checkCallExpression (..\node_modules\typescript\lib\typescript.js:36836:29)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38213:28)
    at Object../src/store/middleware.ts (http://localhost:8080/static/js/main.js:69038:7)
    at __webpack_require__ (http://localhost:8080/static/js/main.js:660:30)

【问题讨论】:

  • 你是在 nodejs 中运行代码还是在浏览器中运行代码? Afaik commonjs 是 nodejs 模块系统。
  • 在浏览器中。我将 Webpack 2.2.0-rc 与 awesome-typescript-loader 3.0.0-beta 一起使用(与 2.1.4 和 ts-loader 相同的错误)。由于 react-router 错误,我无法使用设置 "module": "ES6"
  • 你知道 webpack 和/或 awesome-ts-loader 是否真的支持在浏览器中加载 commonjs 模块吗?因为'exports'是一个必须由模块加载器提供的对象,根据你得到的错误,你得到的对象显然是错误的。
  • 是的,webpack 支持 commonjs:webpack.js.org/concepts/modules 错误似乎来自 TypeScript 编译器
  • 添加了来自浏览器控制台的堆栈跟踪。你是对的,从外观上看,它可能与 Webpack 2 有关。

标签: typescript compiler-errors webpack react-redux typescript2.0


【解决方案1】:

感谢 Martin,我发现这实际上是 Webpack 2 的问题。切换到 Webpack 1 为我解决了这个问题。

【讨论】:

    猜你喜欢
    • 2023-01-15
    • 2022-01-22
    • 2022-01-20
    • 2018-07-18
    • 2016-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 2018-03-13
    相关资源
    最近更新 更多