question-mark

Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Importing individual method fails with "Class is not a constructor"

See original GitHub issue

Issue Description

I’d like to import only the dereference method (the other parts of this module should ideally be tree-shaken out), but it seems this is currently not possible because of this error:

> const {dereference} = require("json-schema-ref-parser")
> dereference({}).then(console.log)
Thrown:
TypeError: Class is not a constructor
    at $RefParser.dereference (node_modules/json-schema-ref-parser/lib/index.js:227:18)

Importing the default export works as expected in comparison:

> const parser = require("json-schema-ref-parser")
> parser.dereference({}).then(console.log)
{}

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

11reactions
KurtPrestoncommented, Oct 11, 2021

json-schema-ref-parser now always fails on TypeScript 4.4 with the same error.

The underlying issue is this: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-4.html#more-compliant-indirect-calls-for-imported-functions

By spec, you cannot bind an exported method, which is causing the code to error on node_modules/@apidevtools/json-schema-ref-parser/lib/index.js:160:

let Class = this; // eslint-disable-line consistent-this
let instance = new Class();

For anyone struggling to get this working, here’s a hacky workaround:

import $RefParser from '@apidevtools/json-schema-ref-parser';
$RefParser.dereference = $RefParser.dereference.bind($RefParser);
$RefParser.resolve = $RefParser.resolve.bind($RefParser);
0reactions
philsturgeoncommented, Nov 26, 2022

Anyone able to get a pull request over to fix this? Or is it not a problem anymore?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to import javascript class. TypeError - node.js
js) imports my socket server module and connects to the socket server. const socket = require('./socket') class Worker { constructor() ...
Read more >
TypeError: "x" is not a constructor - JavaScript - MDN Web Docs
The JavaScript exception "is not a constructor" occurs when there was an attempt to use an object or a variable as a constructor,...
Read more >
Built-in Exceptions — Python 3.11.1 documentation
Two exception classes that are not related via subclassing are never ... while others are usually called only with a single string giving...
Read more >
ES6 Class Mocks - Jest
Jest can be used to mock ES6 classes that are imported into files you want to test. ES6 classes are constructor functions with...
Read more >
Classes and Modules | Advanced JavaScript
The constructor, if not necessary for a JavaScript class, but there can only be one method with the name constructor in a class....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found