Gulp, Node.js and "require.extensions.hasOwnProperty is not a function"
I've been working through getting an older version of an AngularJS project to run locally and encountered the following error:
D:\Git\MyApp>gulp dev
D:\Git\MyApp\node_modules\require-dir\index.js:93
if (!require.extensions.hasOwnProperty(ext)) {
^
TypeError: require.extensions.hasOwnProperty is not a function
at requireDir (D:\Git\MyApp\node_modules\require-dir\index.js:93:37)
at Object.<anonymous> (D:\Git\MyApp\node_modules\gulp-git\index.js:4:18)
at Module._compile (module.js:641:30)
at Object.Module._extensions..js (module.js:652:10)
Which had me scratching my head for a few minutes! A quick beyond compare of the content of the require-dir npm content between the version of the app I was trying to get working, and the latest version of the app made it quite apparent what the problem is:
The code on the line in question went from:
// again protect against enumerable object prototype extensions:
if (!require.extensions.hasOwnProperty(ext)) {
To:
// Node v8+ uses "clean" objects w/o hasOwnProperty for require
// again protect against enumerable object prototype extensions:
if (!Object.prototype.hasOwnProperty.call(require.extensions, ext)) {
The clue here being that the latest version of the app (a) uses a later version of require-dir, and (b) uses a later version of Node.js. Solution: In order to minimise change, revert back to an earlier version of Node.js and carry on from there!