Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Object prototype may only be an Object or null: undefined #40

Open
vtrphan opened this issue Mar 9, 2018 · 3 comments
Open

Comments

@vtrphan
Copy link

vtrphan commented Mar 9, 2018

Hello,

i'm getting this error. Typescript 2.7.2
TypeError: Object prototype may only be an Object or null: undefined
at setPrototypeOf (native)
[Node] at __extends

Any idea why?

@bjyoungblood
Copy link
Owner

I wasn't able to duplicate this in a project using TypeScript 2.7.2.

Can you provide a code sample that causes this error?

@andrewc89
Copy link

I am seeing this error as well in Typescript 2.7.2. It appears when I run tests with the following command:

mocha -r ts-node/register -r tsconfig-paths/register test/**/*.test.ts

Here is a (hopefully useful) snippet of the stack trace:

at Module._compile (module.js:662:30)
    at Module.m._compile (C:\Dev\senalysis-common\node_modules\ts-node\src\index.ts:402:23)
    at Module._extensions..js (module.js:673:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Dev\senalysis-common\node_modules\ts-node\src\index.ts:405:12)
    at Module.load (module.js:575:32)
    at tryModuleLoad (module.js:515:12)
    at Function.Module._load (module.js:507:3)
    at Module.require (module.js:606:17)
    at require (internal/module.js:11:18)

Here is the class from which the error is thrown:

import ExtendableError from 'es6-error';

import {FacebookApiError} from 'models/facebook';

export class FacebookError extends ExtendableError {
    fbTraceId?: string;

    constructor(error: FacebookApiError) {
        super(error.error_user_msg);
        this.fbTraceId = error.fbtrace_id;
    }
}

I copied the ExtendableError internals into my own class, BaseError, and extended that instead. This gives no error.

export class BaseError extends Error {
    constructor(message = '') {
        super(message);
    
        // extending Error is weird and does not propagate `message`
        Object.defineProperty(this, 'message', {
          configurable: true,
          enumerable : false,
          value : message,
          writable : true,
        });
    
        Object.defineProperty(this, 'name', {
          configurable: true,
          enumerable : false,
          value : this.constructor.name,
          writable : true,
        });
    
        if (Error.hasOwnProperty('captureStackTrace')) {
          Error.captureStackTrace(this, this.constructor);
          return;
        }
    
        Object.defineProperty(this, 'stack', {
          configurable: true,
          enumerable : false,
          value : (new Error(message)).stack,
          writable : true,
        });
      }
}
import {BaseError} from './baseError';
import {FacebookApiError} from 'models/facebook';

export class FacebookError extends BaseError {
    fbTraceId?: string;

    constructor(error: FacebookApiError) {
        super(error.error_user_msg);
        this.fbTraceId = error.fbtrace_id;
    }
}

@cpylua
Copy link

cpylua commented Mar 13, 2019

TL;DR: Turn on esModuleInterop compiler option. If this is not an option, go ahead copy-and-paste.

Take at a look at this post.

babel-plugin-add-module-exports doesn't play well with TypeScript.

Hope it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants