Compiler warning "Module not found: Error: Can't resolve './locale'" since I use momentjs in my react app

时间:2017-10-12 09:38:10

标签: reactjs typescript npm webpack momentjs

I get the following warning since I use moment.js in my application (npm 5.4.2, react 15.4, typescript 2.5.3, webpack 2.2.1, webpack-dev-server 2.4.1).

In ./src/utils/commons.ts I use momentjs and it's working fine, but I get the warning.

Any help appreciated.

WARNING in ./~/moment/src/lib/locale/locales.js
Module not found: Error: Can't resolve './locale' in 'C:\myproject\node_modules\moment\src\lib\locale'
 @ ./~/moment/src/lib/locale/locales.js 65:16-60
 @ ./~/moment/src/lib/locale/locale.js
 @ ./~/moment/src/moment.js
 @ ./src/utils/commons.ts
 @ ./src/components/Login.tsx
 @ ./src/app.tsx
 @ ./src/index.tsx
 @ multi (webpack)-dev-server/client?http://localhost:3000 webpack/hot/dev-server react-hot-loader/patch webpack-dev-server/client?http://localhost:3000 webpack/hot/only-dev-server ./src/index.tsx
webpack: Compiled with warnings.

2 个答案:

答案 0 :(得分:1)

Add this in your webpack.config.js under plugins:

new webpack.IgnorePlugin(/^\.\/locale$/, [/moment$/])

答案 1 :(得分:1)

This is a bug in Moment v2.19, due to changes in require: Issue on Github

You can lock out 2.18.1 version in your package.json or apply fixes, mentioned in the link.