tweeeetyのぶろぐ的めも

アウトプットが少なかったダメな自分をアウトプット<br>\(^o^)/

【node.js】npmで`TypeError: require.extensions.hasOwnProperty is not a function`というエラーで怒られる

はじめに

f:id:tweeeety:20180608202233p:plain

タイトルのまんまですが、 とあるプロジェクトでnpm runスクリプトを走らせると以下のようなエラーが出たのでその対処方メモ

# nodeのversionをあげる
$ nodebrew install v8.11.0
$ nodebrew use v8.11.0

# 念のため削除
$ rm -rf node_modules
$ rm -rf ~/.npm

# いれなおす
$ npm install

# versionをあげる前は動いていたが
# npmスクリプトでエラー
$ npm run sample

> hoge@1.0.0 sample /Users/tweeeety/hoge
> gulp

/Users/tweeeety/hoge/node_modules/require-dir/index.js:97
            if (!require.extensions.hasOwnProperty(ext)) {
                                    ^

TypeError: require.extensions.hasOwnProperty is not a function
    at requireDir (/Users/tweeeety/hoge/node_modules/require-dir/index.js:97:37)
    at Object.<anonymous> (/Users/tweeeety/hoge/gulpfile.js:2:1)
    at Module._compile (module.js:612:30)
    at Object.Module._extensions..js (module.js:623:10)
    at Module.load (module.js:531:32)
    at tryModuleLoad (module.js:494:12)
    at Function.Module._load (module.js:486:3)
    at Module.require (module.js:556:17)
    at require (internal/module.js:11:18)
    at Liftoff.handleArguments (/Users/tweeeety/hoge/node_modules/gulp/bin/gulp.js:116:3)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! hoge@1.0.0 compile: `gulp`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the hoge@1.0.0 compile script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/tweeeety/.npm/_logs/2018-06-08T04_01_16_000Z-debug.log

このときの環境

$ ruby -v
ruby 2.4.4p296 (2018-03-28 revision 63013) [x86_64-darwin17]

$ node -v
v8.11.0

$ npm -v
3.8.6

$ rbenv -v
rbenv 1.1.1

$ nodebrew list
v5.12.0
v8.11.0

current: v8.11.0

なにがおこったか

根本原因

githubのissueにあがってました

https://github.com/tjunnone/npm-check-updates/issues/363

nodeのmoduleのrequire-dirを古いまま使い続けているからぽいです。

自分のところの原因

nodeを8.x.xなどの新しいものにあげましたが、
package.jsonでバージョンを固定したモジュールが古いままだったためでした。

  • nodeを8.x.xにあげた
  • 固定していたmodule(今回でいうrequire-dir)のversionが古いままで8.x.xに対応していないversionのままだった

というかんじ

確認する

まずは今のrequire-dirのversionを確認してみました。
https://github.com/aseemk/requireDir/releases

たしかに0.3.2 -> 1.0.0とあがってます。

f:id:tweeeety:20180608202312p:plain

次に、このプロジェクトで使っているsouce-mapを確認してみます。

# 0.3.0でした
# それが悪いってことでもないけど...
$ npm list require-dir
hoge@1.0.0 /Users/tweeeety/hoge
└── require-dir@0.3.0

対応

では、どうやってバージョンをあげたかというと
今回はncu(npm-check-updates)require-dir以外も一括更新しました。

ncuに関しては以下をご参照ください。

doing!

# nodeのversionをあげる
$ nodebrew install v8.11.0
$ nodebrew use v8.11.0

# 念のため削除
$ rm -rf node_modules
$ rm -rf ~/.npm

# 入ってなければいれる
$ npm uninstall npm-check-updates -g && npm install npm-check-updates -g

# 更新実行
$ ncu
$ ncu -u

# いれなおす
$ npm install

# エラーなくなった!
$ npm run sample

参考

以下のサイトも参考にさせていただきました!thx!

おわり

すぐわかればイイですがこういう系ってハマると長いですよね\(^o^)/