LoginSignup
2
2

More than 5 years have passed since last update.

moment.min.jsをrequireしたらModule not foundになった

Last updated at Posted at 2017-11-15

いろいろモジュールをrequireしてたらサイズがでかくなってしまったので、圧縮されてるものは圧縮ファイルを使おうとしたらハマったのでメモ。

元のプログラム

const moment = require('moment');
const xxxx = reuqire('xxxxx');
...

という風にいろいろ圧縮されてないやつを使ってたらサイズが一気に増えてしまった。

圧縮されてるモジュールはそっちを使おうかと、でかいものから順に圧縮されたものを使ってたらmomentで引っかかりました。

momentの圧縮ファイルを使う

momentの読み込み部分を以下のように変更

const moment = require('moment/min/moment.min.js');

これだけ。

Module not found

npm起動して動作確認しようとしたら「Module not found」の文字が目に入った。

その一文を書くと

Module not found: Error: Can't resolve './locale' in '/xxx/yyy/node_modules/moment/min'

というエラーが出現。

「ファイルの中身をこう直せばいいよ!」っていう記事はたくさんあったけど、極力元のコードは触りたくない。

しかも動作はちゃんとできてるので、なんとかそのエラーを消せないものかと奮闘(ぐぐる)。

で、公式のgithub issuesに対策がありました。

プロジェクトwebpack.config.jsのplugins欄に

// webpack.config.js
plugins: [
    ...
    new webpack.IgnorePlugin(/\.\/locale$/)
]

のように書けばおkとのこと。

無視してるだけだから根本的な解決にはなってないと思うけど、とりあえず動作してればいいのでこれを採用。

無事、Module not foundは消えました。

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2