[英]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.