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.

Provide "default" Ajv as a named export

See original GitHub issue

What version of Ajv you are you using? 7.2.4

What problem do you want to solve?

tldr: named + default exports are not simultaneously possible with CJS. Pick one (probably named).

The default export of Ajv is the Ajv class https://github.com/ajv-validator/ajv/blob/master/lib/ajv.ts#L37

which implies equivalent ESM code like import Ajv from 'ajv'.

That works in babel/typecript environments that transform the code into something like const Ajv = require('ajv').default

But with NodeJS’s native ESM, default export/import works a bit differently: https://nodejs.org/api/esm.html#esm_import_statements

The “default” export is actually module.exports, and accessing the intended “default” export requires:

import DefaultAjv from 'ajv';
const Ajv = DefaultAjv.default;

At best that is ugly, at worst it is actively broken when writing the code with Typescript+ESM.

This can be checked by running import('ajv').then(console.log) and notice the output is something like { default: { default: Ajv } }

What do you think is the correct solution to problem? The simplest solution is probably to provide Ajv as a named export. e.g. add export { Ajv } to the main file.

That allows pure ESM solutions like import { Ajv } from 'ajv', and this would be non-breaking.

Alternatively it can use a correct default export, and export the Ajv class as module.exports = Ajv. This would mean any other named exports have to be tacked onto the Ajv class rather than exported in parallel.

Will you be able to implement it? Happy to open a PR and update docs to match, just wanted to expose reasoning as well.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
epoberezkincommented, Mar 31, 2021

See the examples in Getting started

1reaction
JacobLeycommented, Mar 31, 2021

You need to call new Ajv() first

Read more comments on GitHub >

github_iconTop Results From Across the Web

ESM module import requires using .default property #1381
Another option would be to export the Ajv class as a named export in addition to the default export: export class Ajv {}...
Read more >
Ajv options - Ajv JSON schema validator
By default Ajv will accept both Date objects and RFC3339 strings. You can specify allowed values with the option timestamp: "date" or timestamp:...
Read more >
does not contain a default export even after being correctly ...
The problem is that you are trying to import a default module (which was exported using export default ), but you didn't export...
Read more >
Use Named Exports over Default Exports in JavaScript
Named exports provide a handful of benefits over default exports. ... A default export can only export a single object, function, ...
Read more >
Untitled
# Standalone validation code Ajv supports generating standalone modules with exported validation function(s), with one default export or multiple named exports, ...
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