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

Improve DX for ESM #953

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Improve DX for ESM #953

wants to merge 1 commit into from

Conversation

karlhorky
Copy link

A quick PR to improve the DX (Developer Experience) for ES Modules (using the import keyword).

Right now, if you use disposable-email-domains using import in Node.js, you receive an error:

// index.js
import domains from 'disposable-email-domains';

Error message:

$ node index.js
node:internal/errors:465
    ErrorCaptureStackTrace(err);
    ^

TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]: Module "file:///home/runner/RepentantTransparentAudacity/node_modules/disposable-email-domains/index.json" needs an import assertion of type "json"
    at new NodeError (node:internal/errors:372:5)
    at validateAssertions (node:internal/modules/esm/assert:82:15)
    at defaultLoad (node:internal/modules/esm/load:24:3)
    at ESMLoader.load (node:internal/modules/esm/loader:407:26)
    at ESMLoader.moduleProvider (node:internal/modules/esm/loader:326:22)
    at new ModuleJob (node:internal/modules/esm/module_job:66:26)
    at ESMLoader.#createModuleJob (node:internal/modules/esm/loader:345:17)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:304:34)
    at async ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:82:21) {
  code: 'ERR_IMPORT_ASSERTION_TYPE_MISSING'
}

You can resolve this error using the { type: 'json' } import assertion as shown below, but this is still experimental:

import domains from 'disposable-email-domains' assert { type: 'json' };

It shows a warning in Node.js 18.12.1 and is not supported at all in older versions:

$ node index.js
(node:376) ExperimentalWarning: Importing JSON modules is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

This PR allows for usage of the simpler import syntax without the import assertion.

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

Successfully merging this pull request may close these issues.

1 participant