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

Error: Cannot find module 'typesafe-i18n/formatters' #71

Closed
nn1k4 opened this issue Jul 29, 2021 · 13 comments
Closed

Error: Cannot find module 'typesafe-i18n/formatters' #71

nn1k4 opened this issue Jul 29, 2021 · 13 comments
Labels
bug Something isn't working help wanted Extra attention is needed

Comments

@nn1k4
Copy link

nn1k4 commented Jul 29, 2021

Now the error appears in developer mode.

yarn dev
Error when evaluating SSR module /src/i18n/formatters.ts:
Error: Cannot find module 'typesafe-i18n/formatters' from 'D:/app/src/i18n'

"typesafe-i18n": "^2.36.0"
"@sveltejs/kit": "^1.0.0-next.138",

@ivanhofer
Copy link
Owner

This seems like an issue within vite. I need to investigate it further when I have more time.
Meanwhile you can copy the formatter functions from here or write your own.

@ivanhofer ivanhofer added bug Something isn't working help wanted Extra attention is needed labels Jul 30, 2021
@nikolay-borzov
Copy link

I assume the error happens because .cjs and .mjs extensions might not be resolved within your configuration.

@ivanhofer
Copy link
Owner

vite does not resolve .cjs and .mjs file extension in ssr mode. I will open a PR next time I'm in front of my laptop.

@ivanhofer
Copy link
Owner

I opened an Issue in the vite repository: vitejs/vite#4452

Workaround:

  • go to node_modules/vite/dist/node/chunks
  • look for the variable ssrExtensions in one of the dep-*.js files
  • add '.cjs' to the array

Then you should be able to run your application again.
It's a one-time fix and only effects your development environment (as far as I can see). Production builds should not be affected.

@es-lynn
Copy link

es-lynn commented Aug 19, 2021

Hi, I'm experiencing this issue after upgrading to npm v7 with Jest.

Using npm v6, I had no issues resolving when running my Jest tests.

After using npm v7 and upgrading my package.lock, I'm no longer able to run my Jest tests.

Cannot find module 'typesafe-i18n/adapters/adapter-react' from 'src/i18n/i18n-react.tsx'

   Require stack:
      src/i18n/i18n-react.tsx
      src/app/AppContext.tsx
      src/components/Button.test.tsx

      2 | /* eslint-disable */
      3 |
    > 4 | import { initI18nReact } from 'typesafe-i18n/adapters/adapter-react'

When I rename the .cjs file to .js, it works fine.

Using the following:

  • npm: 7.21.0
  • ts-jest: 26.5.4
  • react-scripts: 4.0.3

@es-lynn
Copy link

es-lynn commented Aug 19, 2021

^regarding the above, I needed a quick fix so I did the following:

  1. Delete package-lock.json
  2. Downgrade to npm v6
  3. npm install with v6 to generate lockfile
  4. Upgrade to npm v7
  5. npm install with v7 to upgrade lockfile

@ivanhofer
Copy link
Owner

Thanks @aelesia for providing a solution.

As far as I can tell, "older" npm packages don't always resolve files correctly.
This package should be compatible with cjs and modern esm formats. I'ts hard to provide a solution that works with everything that is out there.. and I really don't want to split this project and provide it's own npm package for each framework.

I think I have configured the package resolving correct, but maybe I have missed something that can be added to support additional frameworks? If someone has additional input to this question, I would be happy :)

@mullwaden
Copy link

Having a similar issue with Jest. I think it might be worth restructuring how the package is imported (not exactly sure how though)

@ivanhofer
Copy link
Owner

@mullwaden have you tried the experimental ESM Support?
https://jestjs.io/docs/ecmascript-modules

@mullwaden
Copy link

Hmm not sure I want to add experimental flags, its a pretty big setup we have. In our case it was the import of the locale detection that was causing issues. I set it to only generate types and made my own base file as we are not using that feature anyway:

import { i18n as initI18n } from "typesafe-i18n";

import type {
  Translation,
  TranslationFunctions,
  Formatters,
  Locales,
} from "./i18n-types";
import { initFormatters } from "./formatters";
import en from "./en";

export const getTranslationForLocale = () => en as Translation;

const i18n = initI18n<Locales, Translation, TranslationFunctions, Formatters>(
  getTranslationForLocale,
  initFormatters
);

export default i18n.en;

@ivanhofer
Copy link
Owner

Good solution!
Jest does not support the modernexports field from package.json and so every import from a object in a sub-folder of typesafe-i18n will fail.

I have found a few issues on that topic.
jestjs/jest#9771
jestjs/jest#10422
jestjs/jest#2702

There are also mentioned a few workarounds, but I haven't checkt if any of these work.

@ivanhofer
Copy link
Owner

The issue vitejs/vite#4452 was fixed in vite version 2.5.2 and therefore also in SvelteKit version 1.0.0-next.160.
Can anyone confirm that the issue is resolved when using the latest version of SvelteKit?
This would make typesafe-i18n again compatible with SvelteKit.

Regarding the issue with jest: can anyone share a repo where I can reproduce the issue?

@ivanhofer
Copy link
Owner

Closing this issue since the original issue was resolved a few months ago.
I think the Jest issue is still there, but I don't know how to reproduce it.
If anyone still has problems, please open a new issue with a detailed description.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

5 participants