[Solved] Cocos Creator 1.4.2: TypeError: Cannot read property 'module' of undefined

Hi,

In Cocos Creator v1.4.0 I was using a JS library file called behaviour3js.

The file starts with:

if (typeof module !== 'undefined' && this.module !== module) {
    var b3 = {};

  module.exports = {
     b3: b3
  };
}

In Cocos Creator v1.4, I was importing this file just fine in my other source files. But once upgraded to Cocos Creator v1.4.2, the editor keeps throwing me this error:

TypeError: Cannot read property 'module' of undefined
    at Object.require.behavior3js: "if (typeof module !== 'undefined' && this.module !== module) {" (assets/src/lib/behavior3js.js:2)

How can I fix this error? Why is ‘this’ undefined?

because this should be undefined in normal scripts, you should import the file as plugin script, see https://github.com/cocos-creator/creator-docs/blob/v1.4/source/en/scripting/plugin-scripts.md

Hi Jare,

Thanks for the link, I will check it out.

I actually re-loaded my project n another instance of Cocos Creator v1.4.0, and this time it threw me the same ‘TypeError: Cannot read property ‘module’ of undefined’ type error.

Now, it’s a mystery to me why it worked in the previous CC v1.4.0 before…

However, I am kinda short on time at the moment so can’t look into it. I solved the error by simply changing the script to:

if (typeof module !== 'undefined' && module.exports) {
    var b3 = {};
    module.exports = {
        b3: b3
    };
}

Which I think actually looks better. :slight_smile: