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 #205

Closed
neviaumi opened this issue Mar 31, 2020 · 27 comments
Closed

TypeError: (0 , _nanoid.nanoid) is not a function #205

neviaumi opened this issue Mar 31, 2020 · 27 comments

Comments

@neviaumi
Copy link

neviaumi commented Mar 31, 2020

I trying upgrade nanoid from 2.x to 3.x in CRA environment

The export value from nanoid is always undefined

And i got error when running below jest test

import { nanoid } from 'nanoid';
it('Should work', () => {
  console.log('nanoid is ', nanoid);
  const genId = nanoid();
  expect(() => genId()).not.toThrow();
});

Console message

console.log src/services/spotify/auth/__tests__/authorize.test.ts:14
    nanoid is  undefined
TypeError: (0 , _nanoid.nanoid) is not a function

Also i try below test

import * as nanoid from 'nanoid';

it('Should work', () => {
  console.log('nanoid is ', nanoid);
  const genId = nanoid.nanoid();
  expect(() => genId()).not.toThrow();
});

Console message

  console.log src/services/spotify/auth/__tests__/authorize.test.ts:14
    nanoid is  { default: 'index.cjs' }
TypeError: nanoid.nanoid is not a function
@neviaumi
Copy link
Author

@TrySound
Copy link

Maybe CRA has some cache? Did you try to rerun it after installing new version?

@TrySound
Copy link

Hm.. this is unexpected nanoid is { default: 'index.cjs' }. v2 does not have this.

@TrySound
Copy link

Perhaps the problem comes from the fact that CRA does not support .cjs extension.

@ai
Copy link
Owner

ai commented Mar 31, 2020

Perhaps the problem comes from the fact that CRA does not support .cjs extension.

CRA should use ESM or even index.browser.js.

Does CRA still use a webpack?

@TrySound
Copy link

@ai
Copy link
Owner

ai commented Mar 31, 2020

Fixing .cjs loading will not fix the problem.

index.browser.js should be loaded according to package.browser

@TrySound
Copy link

@ai
Copy link
Owner

ai commented Apr 1, 2020

@davidNHK I cloned your project, updated Nano ID by yarn upgrade-interactive --latest, replaced import nanoid from to import { nanoid } from and everything starts to work.

In your pull request, you didn’t replace import nanoid from import { nanoid } from`.

Can you try to do it again with updating imports according to the migration guide. If you will have any problem, just give me a link to git branch to reproduce the error.

@neviaumi
Copy link
Author

neviaumi commented Apr 1, 2020

@davidNHK I cloned your project, updated Nano ID by yarn upgrade-interactive --latest, replaced import nanoid from to import { nanoid } from and everything starts to work.

In your pull request, you didn’t replace import nanoid from import { nanoid } from`.

Can you try to do it again with updating imports according to the migration guide. If you will have any problem, just give me a link to git branch to reproduce the error.

@ai

I attempt follow the migration guide

But problem still exist, PR here

P.S. i also attempt change index.browser to index.web and no luck

@baeharam
Copy link

baeharam commented Apr 2, 2020

Same issue here

@ai
Copy link
Owner

ai commented Apr 2, 2020

@davidNHK everything works in the web, problem realted with Create React App test runner.

@ai
Copy link
Owner

ai commented Apr 2, 2020

I found the problem in the CRA jest config generator. Wait for a second, I will create pull request to CRA.

@ai
Copy link
Owner

ai commented Apr 2, 2020

I created pull request facebook/create-react-app#8768

Please add 👍 if you want to merge it quicker

@ai
Copy link
Owner

ai commented Apr 2, 2020

I also added note about CRA to docs and migration guide

@ai
Copy link
Owner

ai commented Apr 5, 2020

My PR was accepting. Now we need to wait a few days for the new CRA release.

@sandrocsimas
Copy link

In node.js this is not working:

const nanoid = require('nanoid');
nanoid();

This works:

const {nanoid} = require('nanoid');
nanoid();

@tareqdayya
Copy link

tareqdayya commented Jul 6, 2020

Any news on this?
For some reason, this problem occurs only when i'm mocking the module globally (inside "__mocks__/nanoid.ts") like this:
export default { nanoid: () => '123456789012', };
But not when i mock it inside individual test suites like this:
jest.mock('nanoid', () => ({ nanoid: () => '123456789012', }));

@ai
Copy link
Owner

ai commented Jul 6, 2020

@tareqdayya Create React App maintainers accepted my PR but still do not release it. Feel free to create an issue in their repo and ask for release.

I can’t help here. Ask CRA maintainers about releasing the fix,

@ai ai closed this as completed Jul 6, 2020
@langdon0003
Copy link

In node.js this is not working:

const nanoid = require('nanoid');
nanoid();

This works:

const {nanoid} = require('nanoid');
nanoid();

I face the same issue and this one work for my case.

Thanks for saving 1 hour.

@alycoy
Copy link

alycoy commented Aug 11, 2020

I'm facing the same problem when using react testing library
TypeError: (0 , _nanoid.nanoid) is not a function

using nanoid version ^3.1.12

@ai
Copy link
Owner

ai commented Aug 11, 2020

@antoniolobo do you use Create React App?

I sent the fix to CRA, but they are still not released it.

@guitheengineer
Copy link

guitheengineer commented Oct 31, 2020

I updated Create React App to 4.0.0 with npm install --save --save-exact [email protected] and it fixed.

See more about migrating to newer CRA versions here.

@guitheengineer
Copy link

Update: solved again an entire different app by updating CRA to 4.0.0. Seems the solution for who is using:

  • Create react app
  • React testing Library

@teaguestockwell
Copy link

I'm having the same issue when running on Code Sandbox. It seems to work fine below version 3.1.17 of nanoid. Is this related, or could it be an issue with Code Sandbox?

@lavrton
Copy link

lavrton commented Nov 16, 2021

Just had the same issue on code sandbox too. I am not sure where to look at.

The demo from @tsAppDevelopment works ok with 3.1.16 and not ok from version 3.1.17

@AvraamMavridis
Copy link

For anyone that is still facing the issue. ESM can’t import named CJS exports unless CJS scripts execute out of order

So you can do this:

import nanoid from 'nanoid'

but you can't do this:

import { nanoid } from 'nanoid'

That’s because CJS scripts compute their named exports as they execute, whereas ESM’s named exports must be computed during the parsing phase.

A workaround is

import _nanoid from 'nanoid'
const { nanoid } = _nanoid

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