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!

About Rob

I've been interested in computing since the day my Dad purchased his first business PC (an Amstrad PC 1640 for anyone interested) which introduced me to MS-DOS batch programming and BASIC.

My skillset has matured somewhat since then, which you'll probably see from the posts here. You can read a bit more about me on the about page of the site, or check out some of the other posts on my areas of interest.

No Comments