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

TypeError: (0 , _nanoid.nanoid) is not a function on v3.3.1 #351

Closed
acelaya opened this issue Mar 27, 2022 · 15 comments
Closed

TypeError: (0 , _nanoid.nanoid) is not a function on v3.3.1 #351

acelaya opened this issue Mar 27, 2022 · 15 comments

Comments

@acelaya
Copy link

acelaya commented Mar 27, 2022

I have just installed latest nanoid (to replace the heavier uuid package on a webpack-based web app), and it seems to work fine at build time and runtime, but when running the tests with jest, it does not seem to properly resolve the module, resulting in TypeError: (0 , _nanoid.nanoid) is not a function.

I have seen this was already reported here #205, but I have tried every workaround proposed there and none of them seem to work now. Since that issue is a bit old, maybe the error is back with a different root cause this time.

I have also tried using jest's moduleNameMapper to manually map to one of the modules from this package. I have tried index.js, index.cjs, index.browser.js and index.browser.cjs, and also, none of them fix the error.

The errors can be seen here https://github.com/shlinkio/shlink-web-client/runs/5708689479?check_suite_focus=true

@ai
Copy link
Owner

ai commented Mar 27, 2022

How do you run your app to get this error?

Is it webpack app? What webpack version do you use?

Do you have this error in browser?

What is your webpack and jest config?

@acelaya
Copy link
Author

acelaya commented Mar 27, 2022

I only get the error with jest. When building it with webpack, it works fine, both dev and prod builds.

I use webpack 5.70, and react-scripts, so the config is whatever they set.

For jest, I do the run myself, with this config https://github.com/shlinkio/shlink-web-client/blob/develop/jest.config.js

@ai
Copy link
Owner

ai commented Mar 27, 2022

Jest has an big. They hack Node.js resolver, but their resolver doesn't respect package.export which leads to errors like this.

@acelaya
Copy link
Author

acelaya commented Mar 27, 2022

True, but that can usually be worked around by using jest's moduleNameMapper config option (you'll notice I'm already doing it for another package, reactstrap).

I have tried manually mapping all the files from this package (at least the ones on the root folder), and it still fails. Is there maybe some other way to map it that I might be missing?

@ai
Copy link
Owner

ai commented Mar 27, 2022

moduleNameMapper is not enough.

Try to create custom resolver jestjs/jest#9771 (comment)

@acelaya
Copy link
Author

acelaya commented Mar 27, 2022

Thanks! I will give that a try

@acelaya
Copy link
Author

acelaya commented Mar 27, 2022

Sadly it does not work with the resolver as it is defined in that comment. I have tried tweaking it a bit, with little success.

Now I'm trying to update to Jest 28 alpha, as supposedly, it now supports package exports. However, other errors are arising. I will report back here if I manage to make it work.

@nemosmithasf
Copy link

nemosmithasf commented Apr 28, 2022

I have the same issue. I've found that the proposed work-around from the earlier thread works:

import { nanoid } from 'nanoid';

However

import { nanoid } from 'nanoid/non-secure';

does not work

@ai
Copy link
Owner

ai commented Apr 29, 2022

Seems like it should be fixed in Jest 28

@ai ai closed this as completed Apr 29, 2022
@nemosmithasf
Copy link

@ai Just a heads up, the error I'm encounter was outside the context of Jest.

@billouboq
Copy link

Yep having same issue in webpack 5. non-secure has "nanoid" is not a function

@Matilop15
Copy link

@billouboq did you find the solution?

@billouboq
Copy link

Well I passed a name props so that portal does not call that nanoid function, so no more crash

@Matilop15
Copy link

Well I passed a name props so that portal does not call that nanoid function, so no more crash

The solution I found was not to use the latest version (4.x) and use a 3.x.x and that's it

@yatessss
Copy link

this works for me
facebook/create-react-app#11889 (comment)

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

No branches or pull requests

6 participants