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

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

Closed
silverwind opened this issue Oct 4, 2019 · 5 comments
Closed

Comments

@silverwind
Copy link

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)
{}
@philsturgeon
Copy link
Member

Is this code of any help? #14 (comment)

@silverwind
Copy link
Author

That example includes a path-based import which would break when the referenced file is moved. I would not consider it a stable interface.

@philsturgeon
Copy link
Member

Yes this is a workaround not a complete solution.

@KurtPreston
Copy link

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);

@philsturgeon
Copy link
Member

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

@jonluca jonluca closed this as completed Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants