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

useCookieValue esm & esnext imports don't work #659

Open
jakubriedl opened this issue Feb 21, 2022 · 8 comments
Open

useCookieValue esm & esnext imports don't work #659

jakubriedl opened this issue Feb 21, 2022 · 8 comments

Comments

@jakubriedl
Copy link

When I try to import useCookieValue from esm or esnext it fails with error SyntaxError: Cannot use import statement outside a module

// works
import { useCookieValue } from "@react-hookz/web/cjs/useCookieValue/useCookieValue"
// fails
import { useCookieValue } from "@react-hookz/web/esm/useCookieValue/useCookieValue"
// also fails
import { useCookieValue } from "@react-hookz/web/esnext/useCookieValue/useCookieValue"
(node:99638) UnhandledPromiseRejectionWarning: /Users/jakubriedl/repositories/cultureamp/unified-home/node_modules/@react-hookz/web/esnext/useCookieValue/useCookieValue.js:2
import { useCallback, useEffect } from 'react';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:12)
    at Module.require (internal/modules/cjs/loader.js:974:19)
    at require (internal/modules/cjs/helpers.js:93:18)
    at Object.95575 (/Users/jakubriedl/repositories/cultureamp/unified-home/apps/unified-home/.next/server/pages/_app.js:709:18)
    at __webpack_require__ (/Users/jakubriedl/repositories/cultureamp/unified-home/apps/unified-home/.next/server/webpack-runtime.js:25:43)
    at Object.12273 (/Users/jakubriedl/repositories/cultureamp/unified-home/apps/unified-home/.next/server/chunks/6501.js:409:26)

Prior Issues

haven't found anything relevant

What is the current behavior?

when building our Next.js app it works when importing cjs but crashes when importing esm or esnext with error SyntaxError: Cannot use import statement outside a module.

I think/guess it is caused by the fact that package.json lists "esnext": "esnext/index.js", "module": "esm/index.js",. Which if I understand what that means is when we import from nested file it isn't recognised as "module" and so it use import syntax.

@xobotyi
Copy link
Contributor

xobotyi commented Feb 21, 2022

🤔 never faced the issue, since i'm transpiling node_modules, and, honestly, have no idea how to work this around.

We're unable to switch to module type, since it will break things for lots of people.

@jakubriedl
Copy link
Author

One option I can think of could be make js-cookie as optional require (like below) and publish useCookieValue in the index

let jsCookie
try {
 jsCookie = require('js-cookie')
} catch(e) {
  throw new Error("`js-cookie` is not installed, but it is required for using `useCookieValue`, see https://react-hookz.github.io/web/?path=/docs/side-effect-usecookievalue--example")
}
// ...

@xobotyi
Copy link
Contributor

xobotyi commented Feb 28, 2022

@jakubriedl barely it is caused by js cookie.

Is any of other hooks viable via esm import?

Reexporting useCookieValue in index will lead to error message for every user even if they're not using it.

@jakubriedl
Copy link
Author

all other hooks that are imported directly from index work for us, its just useCookieValue.

What kind of error would users experience if it would be exported in index and using optional require?

@jakubriedl
Copy link
Author

I've recently come across exports field in package.json which might be the way how to solve that.

@xobotyi
Copy link
Contributor

xobotyi commented Mar 8, 2022

@jakubriedl it is only suitable for "type": "module" packages, that we won't be using anytime soon.

@ryanlalchand
Copy link

@jakubriedl @xobotyi I have the exact same problem, and only the CJS import works.

I would like to add that also following the docs, import { useCookieValue } from "@react-hookz/web"; yields Module '"@react-hookz/web"' has no exported member 'useCookieValue'.

@xobotyi
Copy link
Contributor

xobotyi commented Jan 13, 2023

It is described in hook docs, that it is not exported from the index file, because of optional dependency.
Since otherwise we'd have to add js-cookie as non-optional dependency and everyone, including ones who not using cookie hook, will have it downloaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants