【问题标题】:Create-React-App : Uncaught Error: Cannot find module "./locale"Create-React-App:未捕获的错误:找不到模块“./locale”
【发布时间】:2017-10-11 15:58:59
【问题描述】:

我有一个使用 Create-React-App 初始化的项目。

如果我...,一切正常。

npm run start 并查看http://localhost:3000

npm run build && serve -s build 并查看http://localhost:5000

但是在我部署到 s3 存储桶后...

npm run build && aws s3 sync build/ s3://${DEPLOYMENT_BUCKET}

...我转到 s3 url 并在控制台中显示此错误的空白屏幕:

未捕获的错误:找不到模块“./locale”

我认为这可能与 moment.js 有关 - 尽管它不是我添加的依赖项。

查看我的 node_modules 我可以看到 moment.js 的版本为 2.18.1,我认为排除了 this answer

我不知道如何调试这个问题,所以任何帮助表示赞赏。

不知道这是否有帮助,但我目前的 package.json 如下。

{
  "name": "my_app",
  "version": "1.0.0",
  "private": true,
  "dependencies": {
    "amazon-cognito-identity-js": "^1.19.0",
    "antd": "^2.13.4",
    "axios": "^0.16.2",
    "react": "^15.6.1",
    "react-dom": "^15.6.1",
    "react-router-dom": "^4.2.2",
    "react-scripts": "1.0.13",
    "recompose": "^0.25.1"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

【问题讨论】:

  • 您是否尝试过将 momentJS 添加为显式依赖项,看看它是否能解决您的问题?至于为什么它被使用和丢失,它可能与您的构建过程有关,这在您的问题中没有详细说明。它可以被您正在使用的软件包之一用作依赖项,然后在构建过程中被忽略。
  • 多么烦人 - 是的,添加时刻作为依赖已修复它 - 干杯 @jaxx
  • 我给出的答案一点帮助都没有?为了您的利益,我希望您在注释掉生产构建配置中的那一行后不必添加此依赖项。
  • 好吧,虽然现在不需要(我刚刚开始这个项目)我已经意识到我实际上最终会使用momentjs,所以我把它弹出来,一切正常。
  • 另外,作为一个 React/JS/NPM 新手,我不知道你是如何注释掉 node_modules/react-scripts/config/webpack.config.prod.js 中的一行代码的

标签: reactjs amazon-s3 create-react-app


【解决方案1】:

当我安装 ant design 依赖项来使用它时,我也面临同样的问题。我得到了同样的错误信息

错误:找不到模块“./locale”

然后我安装moment然后卸载它,这个bug也随之消失了。

npm install moment@2.24.0

npm uninstall moment

【讨论】:

    【解决方案2】:

    在研究了 react-scripts 处理项目构建的方式之后,我在 create-react-app 附带的 /react-scripts/config/webpack.config.prod.js 文件中发现了这个有趣的代码 sn-p(这是用于产品构建的 Webpack 配置文件) .

    // Moment.js is an extremely popular library that bundles large locale files
    // by default due to how Webpack interprets its code. This is a practical
    // solution that requires the user to opt into importing specific locales.
    // https://github.com/jmblog/how-to-optimize-momentjs-with-webpack
    // You can remove this if you don't use Moment.js:
    new webpack.IgnorePlugin(/^\.\/locale$/, /moment$/),
    

    您可能希望导入特定的语言环境(如果您使用任何语言环境,但似乎并非如此),或者注释掉该行。

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 1970-01-01
      • 2021-04-02
      • 2018-07-21
      • 1970-01-01
      • 2019-08-17
      • 2022-01-24
      • 2021-04-30
      • 2022-07-20
      相关资源
      最近更新 更多