简体   繁体   English

使用 Moment JS 创建 React-App:找不到模块“./locale”

[英]Create-React-App with Moment JS: Cannot find module “./locale”

Just ran an npm update on my web application and now Moment JS appears to be failing with the following message:刚刚在我的 Web 应用程序上运行了npm update ,现在Moment JS似乎失败并显示以下消息:

Error: Cannot find module "./locale"
\node_modules\moment\src\lib\moment\prototype.js:1
> 1 | import { Moment } from './constructor';

Not sure what version of Moment JS I had prior to my update, but my application has been working for months.不确定更新之前我使用的是哪个版本的Moment JS ,但我的应用程序已经运行了几个月。

I created another react app and ran an npm install moment --save and modified the source to display the time and ended up with the same error described above.我创建了另一个 react 应用程序并运行了npm install moment --save并修改了源代码以显示时间并最终出现上述相同的错误。

Not sure if there is a fail-proof way to integrate Moment JS using Create-React-App currently short of ejecting to manage the webpack settings myself, but I really don't want to do this.不确定是否有一种防故障方法可以使用Create-React-App集成Moment JS,目前还没有弹出来自己管理 webpack 设置,但我真的不想这样做。 Anyone else seeing these issues or having success?还有其他人看到这些问题或取得成功吗? If so, a short write up would go along way to helping out.如果是这样,写一篇简短的文章将有助于提供帮助。

Appears this has already been identified as an issue for Moment JS version 2.19.看来这已经被确定为Moment JS 2.19 版的问题。 If you have upgraded to 2.19 run npm install moment@2.18.1 to revert back to previous version until it is fixed!如果您已升级到 2.19,请运行npm install moment@2.18.1以恢复到以前的版本,直到它被修复!

See thread: https://github.com/moment/moment/issues/4216见线程: https : //github.com/moment/moment/issues/4216

Application built with Create React App and using Moment 2.24.0, the following seems to be working fine:使用 Create React App 和 Moment 2.24.0 构建的应用程序,以下似乎工作正常:

import moment from 'moment';
import 'moment/min/locales';

Instead of importing moment-with-locales directly.而不是直接导入moment-with-locales Alternatively, also possible to only import required locales:或者,也可以只导入所需的语言环境:

import moment from 'moment';
import 'moment/locale/fr';
import 'moment/locale/ru';

The answer above, though I have no doubt works for some, does not work for me.上面的答案,虽然我毫不怀疑对某些人有用,但对我不起作用。 The solution I found is fairly quick and easy, but is a little more complicated than simple downgrading.我找到的解决方案相当快速和简单,但比简单的降级要复杂一些。

This problem originates as a result of and can be fixed with webpack.这个问题源于 webpack,可以用 webpack 修复。 So we're going to have to add a few lines of code to our webpack.config.js file.所以我们将不得不在我们的 webpack.config.js 文件中添加几行代码。 If you don't have one yet, you can add one to the root webpack directory:如果你还没有,你可以在根 webpack 目录中添加一个:

YOURAPPNAME/node-modules/webpack/

So now that you're inside your webpack.config.js file, you need to add a few lines of code.所以现在你在你的 webpack.config.js 文件中,你需要添加几行代码。 Just copy and paste this inside the new file or adapt it to the code you already have in webpack.config.js.只需将其复制并粘贴到新文件中或将其调整为您在 webpack.config.js 中已有的代码。

module.exports = {
    resolve: {
        alias: {
            moment$: 'moment/moment.js'
        }
    }
};

Your import statement of moment in your index.js (or otherwise named) file should look like this:您在 index.js(或其他名称)文件中的 import 语句应如下所示:

import moment from 'moment'

You should now be able to use moment completely normally.您现在应该可以完全正常使用 moment。 For example:例如:

var tomorrow = moment().add(1, "day")

If you have a fresh install, or upgraded moment to 2.25 and are getting this warning now, try forcing all your packages to use 2.24.如果您有全新安装或升级到 2.25 并且现在收到此警告,请尝试强制您的所有软件包使用 2.24。

UPDATE: New release 2.25.3 has been reported to fix this problem!更新:已报告新版本 2.25.3解决了这个问题! Try to first just update.尝试先更新。

If you depend on some library that didn't upgrade yet, tell them to upgrade.如果您依赖某个尚未升级的库,请告诉他们升级。 And in the meantime, if you need 2.25, you can force also your sub-dependencies to use this version.同时,如果您需要 2.25,您也可以强制您的子依赖项使用此版本。

If you're using yarn add these lines into package.json如果您使用yarn将这些行添加到 package.json

    "resolutions": {
        "**/moment": ">=2.25.3"
    },

This is to be added inside packages.json at the top level, ie with the same indentation as "dependencies".这将被添加到顶层的packages.json 中,即与“依赖项”具有相同的缩进。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 create-react-app 错误:找不到模块“react-scripts/scripts/init.js” - create-react-app Error: Cannot find module 'react-scripts/scripts/init.js' 创建反应应用程序错误:找不到模块'./locale' - Create react app Error: Cannot find module './locale' Jest.js 和 Create-react-app:运行测试时出现“语法错误:无法在模块外使用导入语句” - Jest.js and Create-react-app: I get "SyntaxError: Cannot use import statement outside a module" when running test create-react-app React Js 的漏洞 - Vulnerabilities with create-react-app React Js 带有 create-react-app 的多模块项目 - Multi module projects with create-react-app 无法使用 create-react-app 创建 React App - Cannot Create React App Using create-react-app Jest 和 Create-react-app:不能在模块外使用 import 语句 - Jest and Create-react-app: Cannot use import statement outside a module 在使用 create-react-app 创建的 reactjs 应用程序中导入 node_module dist js 文件 - Import a node_module dist js file in reactjs application created using create-react-app 尝试将 jest 与 create-react-app 和 typescript 一起使用。 获取错误:开玩笑:无法解析 TypeScript 配置文件...找不到模块“ts-node” - Trying to use jest with create-react-app and typescript. Get error: Jest: Failed to parse the TypeScript config file... Cannot find module 'ts-node' 无法使用 create-react-app 创建新的 React 应用程序 - Cannot create a new React application with create-react-app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM