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

ESM imports are failing #253

Closed
2 tasks done
gajus opened this issue Sep 18, 2023 · 14 comments · Fixed by #254
Closed
2 tasks done

ESM imports are failing #253

gajus opened this issue Sep 18, 2023 · 14 comments · Fixed by #254

Comments

@gajus
Copy link

gajus commented Sep 18, 2023

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Fastify version

N/A

Plugin version

9.0.4

Node.js version

19.9.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

13.2.1

Description

Importing in ESM workspace is failing with an error:

import { fastifyCookie } from '@fastify/cookie';
         ^^^^^^^^^^^^^
SyntaxError: Named export 'fastifyCookie' not found. The requested module '@fastify/cookie' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from '@fastify/cookie';
const { fastifyCookie } = pkg;

Steps to Reproduce

Add "type": "module" to package.json.

Expected Behavior

Should work with ESM imports.

@gurgunday
Copy link
Member

There isn’t a named export called fastifyCookie

@gurgunday
Copy link
Member

import pkg from '@fastify/cookie';
const { fastifyCookie } = pkg;

should work

@gajus
Copy link
Author

gajus commented Sep 18, 2023

But that fails TypeScript types.

@gajus
Copy link
Author

gajus commented Sep 18, 2023

For context, const fastifyCookie = await import('@fastify/cookie') works as expected.

@gurgunday
Copy link
Member

For context, const fastifyCookie = await import('@fastify/cookie') works as expected.

If you're looking for the equivalent of this import, it would be the following:

import fastifyCookie from '@fastify/cookie';

It uses default exports - notice how there aren't curly brackets ({})

@gajus
Copy link
Author

gajus commented Sep 18, 2023

Ah, misread the error indicators in VSCode.

import fastifyCookie from '@fastify/cookie';

The error is coming from ESLint:

Using exported name 'fastifyCookie' as identifier for default export.eslint[import/no-named-as-default](https://github.com/import-js/eslint-plugin-import/blob/v2.28.1/docs/rules/no-named-as-default.md)

https://github.com/import-js/eslint-plugin-import/blob/v2.28.1/docs/rules/no-named-as-default.md

i.e. Looks like ESLint thinks that there is fastifyCookie named export, but there isn't.

@gajus
Copy link
Author

gajus commented Sep 18, 2023

I can only assume that this is because of:

export const fastifyCookie: FastifyCookie;

which exists inside of the namespace fastifyCookie.

I am not familiar with TypeScript namespaces, so cannot comment whether this is an issue with this package or ESLint rule.

@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 19, 2023

According to the eslint rule, this is a warning for you as an end user, as it could be that you wanted to use the named import and not the default import. Nothing to do with us.

@gajus
Copy link
Author

gajus commented Sep 19, 2023

But there is no named export.

@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 19, 2023

fastifyCookie.fastifyCookie = fastifyCookie

@gajus
Copy link
Author

gajus commented Sep 19, 2023

So that takes us to the original issue.

import { fastifyCookie } from '...'

Does not work

@Uzlopak
Copy link
Contributor

Uzlopak commented Sep 19, 2023

Please provide a repo

@gajus
Copy link
Author

gajus commented Sep 19, 2023

There isn’t a named export called fastifyCookie

This was already confirmed to be an issue.

@gurgunday
Copy link
Member

fastifyCookie.fastifyCookie = fastifyCookie

That will not work if that was the intention. To do an ESM named export, a property of module.exports must be set like so: module.exports.fastifyCookie = fastifyCookie

I haven't properly looked at the code but ESLint might be mistakenly thinking that a named export exists while it doesn't. Why do you import it like that anyway? This is the de-facto way of registering plugins/routes:

// await is optional *here*
await fastify.register(import("@fastify/cookie"), {
});

@gurgunday gurgunday mentioned this issue Sep 19, 2023
4 tasks
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 a pull request may close this issue.

3 participants