ai / nanoid

A tiny (124 bytes), secure, URL-friendly, unique string ID generator for JavaScript
https://zelark.github.io/nano-id-cc/
MIT License
24.57k stars 791 forks source link

Typescript / Mocha / V4 Issue #394

Closed rzec-allma closed 1 year ago

rzec-allma commented 2 years ago

I am using nanoid v4 in a NextJS application and while the library works fine in the application itself, when I run any unit tests that include code that using nanoid, I get this error:

TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for /Users/ryanzec/repositories/allma-web-app/mocha-setup.ts

My tsconfig.json is:

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "types": ["cypress", "cypress-real-events"],
    "paths": {
      "$/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules", "**/*.spec.*"]
}

I know there is a similar issue related to v4 in node however that issue in that seems to be related specifically to importing not working and not the error I am getting. The unit tests work fine with v3 so I assume it is related to the ESM change though not sure why it is not working since I am using esnext in my tsconfig.json.

While I can and will use v3, just wanted to point this out to see if there is something I am doing wrong or something that needs to be fixed with the library itself.

ai commented 2 years ago

Sorry, I have no idea and the environment stack here is too big for debug.

Did you change any config during the update?

rzec-allma commented 2 years ago

Nope, the only difference between working and not working is v3.3.4 (working) and v4.0.0 (not working) of nanoid.

ai commented 2 years ago

Sorry, still have no idea.

Let’s keep it open, maybe somebody will know the answer.

Don’t worry to use v3, it is officially supported.

Windovvsill commented 2 years ago

Same issue here. Upgrading to 4.0.0 causes ts-mocha (which uses ts-node) to throw Error: unknown file extension .ts

It could be related to the commit where "type": "module" was added to package.json (4f5544255f3aba912c7662aca0434a4506953c5c), as some commenters posted in this thread https://github.com/TypeStrong/ts-node/issues/1062.

Downgrading to 3.3.4 removes the errors, and my tests run again.

SuperCipher commented 2 years ago

I have this exact issue. Downgrading the nanoid from v4 to v3.3.4 solved it.