gpt4 book ai didi

javascript - react 16.14.0 : Error was not caught ReferenceError: exports is not defined

转载 作者:行者123 更新时间:2023-12-03 16:18:16 25 4
gpt4 key购买 nike

我是next-translate的作者库,我正在开发一个实验版本,我得到了 React 16.14.0 出错 我不明白为什么会这样。 将 React 升级到版本 17 然后它可以正常工作 ,但我不想强制所有使用我库的新版本的人迁移他们的 React 版本。
我创建了一个可重现的错误示例:
https://github.com/aralroca/next-translate-error-reproduction
为了重现此问题:

  • 克隆这个 repo
  • 运行yarn && yarn dev
  • 打开localhost:3000
  • 打开开发工具
  • Error was not caught ReferenceError: exports is not defined我的库的预发布代码在这里:
  • https://github.com/vinissimus/next-translate/tree/1.0.0-experimental.14

  • tsconfig.json
    {
    "compilerOptions": {
    "strict": false,
    "module": "es6",
    "target": "es5",
    "jsx": "react",
    "removeComments": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "declaration": true,
    "lib": ["esnext", "dom"],
    "allowJs": true,
    "forceConsistentCasingInFileNames": true,
    "outDir": "./lib/esm"
    },
    "include": ["./src"]
    }
    tsconfig-cjs.json
    {
    "extends": "./tsconfig.json",
    "compilerOptions": {
    "module": "CommonJS",
    "outDir": "./lib/cjs",
    "declaration": false
    }
    }
    这是包代码:
  • https://www.runpkg.com/?next-translate@1.0.0-experimental.14

  • 如果有人知道这个错误来自哪里并且可以帮助我,我将非常感激!几天来我一直在尝试解决它,但我有点迷茫,因为我不知道为什么会这样......谢谢!
    注意:看起来它只发生在开发中,做 yarn build && yarn start工作正常。

    最佳答案

    我已经修复了它,但如果我说实话,我不明白为什么......让我们看看是否有人可以向我解释!
    我正在调试,发现是我的库代码中的某些内容导致 TypeScript 编译错误。因此,我逐行取消注释,直到我看到该行有什么问题:

    import App from 'next/app'

    // Inside getInitialProps wrapper...
    App.getInitialProps() // THIS LINE
    好像评论时 App.getInitialProps()然后 TypeScript 它编译得很好。幸运的是,我已经能够在我的库中省去这行代码,现在有了预发行版 1.0.0-experimental.15它似乎与 React 16 配合得很好。
    我真的不明白为什么会发生这种情况,可能是因为我将 Next.js 作为 peerDependency 吗?我希望有人可以为我澄清为什么会这样🙏

    关于javascript - react 16.14.0 : Error was not caught ReferenceError: exports is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64875443/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com