Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NODE 8.0.0 and NPM 4.2.0 ERROR express-load require.extensions.hasOwnProperty is not a function

Tags:

node.js

npm

I am creating an app in Node 8.0.0 and NPM 5.0.0 and when i using express-load var load = require("express-load"); my server returns:

load/lib/express-load.js:32

  if (require.extensions.hasOwnProperty(ext) && extlist.indexOf(ext) === -1) {
                         ^

TypeError: require.extensions.hasOwnProperty is not a function
    at Object.<anonymous> (/Users/node_modules/express-load/lib/express-load.js:32:26)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/node_modules/express-load/index.js:8:18)
    at Module._compile (module.js:569:30)

I tried downgrade to npm sudo npm install -g [email protected] version 4.2.0 (https://github.com/tjunnone/npm-check-updates/issues/355) and does not works

This post npm check updates works to me.

thanks!!!!

like image 423
Marcus Menezes Avatar asked Jun 01 '17 03:06

Marcus Menezes


1 Answers

None of the answers posted so far explain why the issue happens and how to fix it.

The Scoop

In Node 6 require.extensions was initialized with {}. In Node 8, it is initialized with Object.create(null).

A test like

if (require.extensions.hasOwnProperty(ext) {

cannot work in Node 8 and higher due to the way require.extensions is initialized with Object.create(null). (Details below.)

The code above has to become:

if (Object.prototype.hasOwnProperty.call(require.extensions, ext)) {

You still have to use hasOwnProperty for compatibility with Node 6. Once compatibility with 6 is dropped, it could be reduced to if (ext in require.extensions) { because the distinction between own and inherited properties is meaningless for objects created by Object.create(null).

If the faulty code is your own, just make the change.

If the faulty code is not your own, then upgrading the package you are using may fix the issue if the devs already took care of fixing the problem. If the problem persists in the latest version of the package, then you need to put in an issue report explaining the problem so that the maintainers fix their code.

Additional Details

What has changed from version 6 to version 8 is the way require.extensions is created.

Version 6: require.extensions gets its value from Module._extensions. (Here.) And Module._extensions is initialized as follows:

Module._extensions = {};

Version 8: require.extensions gets is value from Module._extensions. (Here.) And Module._extensions is initialized as follows:

Module._extensions = Object.create(null);

Now run the following code:

// This is fine.
console.log({}.hasOwnProperty("foo"));

// This will fail.
console.log(Object.create(null).hasOwnProperty("foo"));

An object created with Object.create(null) does not have the hasOwnProperty method. In fact an object newly created with Object.create(null) has no property whatsoever defined on it, because it does not inherit from anything. This is the appeal of such objects because then you don't have to worry about own properties and inherited properties. "toString" in {} is true because it is inherited from Object whereas "toString" in Object.create(null) is false.

If you want to use hasOwnProperty on it, most likely for compatibility with older versions that may initialize an object you don't control, you have to acquire the method from the Object class:

const obj = Object.create(null);
console.log(Object.prototype.hasOwnProperty.call(obj, "foo"));
like image 75
Louis Avatar answered Oct 15 '22 10:10

Louis