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

PrimeReact: Can't resolve 'primelocale' in v2 #218

Open
juliecki opened this issue Mar 4, 2025 · 9 comments
Open

PrimeReact: Can't resolve 'primelocale' in v2 #218

juliecki opened this issue Mar 4, 2025 · 9 comments
Assignees
Labels
help wanted Extra attention is needed question Further information is requested

Comments

@juliecki
Copy link

juliecki commented Mar 4, 2025

I've upgraded from v1.6.0 -> v2.0.1.
Since then, I cannot import the needed primelocale dynamically.

It's used like this:

export const dynamicPrimeReactLocaleActivate = async (
  currentLocale: string,
) => {
  const localeFile = await import(`primelocale/${currentLocale}.json`);
  addLocale(currentLocale, localeFile[currentLocale]);
  locale(currentLocale);
};

But then I get the Error: Module not found: Error: Can't resolve 'primelocale'

PrimeReact: "primereact": "10.9.1"
React: "react": "18.3.1"
TypeScript: "typescript": "5.7.2"

@melloware melloware changed the title Can't resolve 'primelocale' in v2 PrimeReact: Can't resolve 'primelocale' in v2 Mar 4, 2025
@melloware
Copy link
Member

melloware commented Mar 4, 2025

@juliecki we now have a way more improved way to load them as JavaScript instead of JSON.

See: https://stackblitz.com/edit/axcck4-nuukjdrw?file=src%2FApp.tsx

@melloware melloware added help wanted Extra attention is needed question Further information is requested labels Mar 4, 2025
@melloware melloware self-assigned this Mar 4, 2025
@juliecki
Copy link
Author

juliecki commented Mar 4, 2025

Thanks!

I now changed it to

export const dynamicPrimeReactLocaleActivate = async (
  currentLocale: string,
) => {
  addLocale(currentLocale, all[currentLocale as keyof AllLocales]);
  locale(currentLocale);
};

But now I get this error:

 Cannot find module 'primelocale'

Is it caused by the modeResolution in tsconfig?

{
  "compilerOptions": {
    "baseUrl": "src",
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
      "esnext.intl",
      "es2017.intl",
      "es2018.intl"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "noFallthroughCasesInSwitch": true
  }

@melloware
Copy link
Member

yeah it looks like thta stackblitz has...

{
  "compilerOptions": {
    "target": "ESNext",
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "module": "ESNext",
    "skipLibCheck": true,
    "allowJs": true,

    /* Bundler mode */
    "moduleResolution": "node",
    "allowImportingTsExtensions": true,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",

    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,

    /* Linting */
    "strict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "noFallthroughCasesInSwitch": true
  },
  "include": ["src"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

@juliecki
Copy link
Author

juliecki commented Mar 5, 2025

Still don't get it work when I configure it like in stackblitz. I saw that the stackblitz project is built with Vite, but we don't have yet migrated to Vite and are still using CRA. I don't know if this is causing issues as well.

@melloware
Copy link
Member

@blutorange any thoughts with CRA? Yeah everything is working fine with Vite and NextJS.

@blutorange
Copy link
Contributor

blutorange commented Mar 5, 2025

On top of my head, without trying it out: Try setting resolvePackageJsonExports to true. We might want to add a main field to our package.json

Do you have minimal reproduction?

@melloware
Copy link
Member

@blutorange we have a simple CRA basic app here https://github.com/primefaces/primereact-examples

you can just grab that and try with PrimeLocale 2.0.1 since its already set up CRA app.

@blutorange
Copy link
Contributor

blutorange commented Mar 5, 2025

cra-basic-ts is broken since the 19.0.0 release of react yesterday, had to change the devDependencies from latest to ^18.0.0.

But anyways, I'm getting the same error with "moduleResolution": "node", but it works with "moduleResolution": "bundler"

@juliecki Can you try editing node_modules/primelocale/package.json and see if it works if you add the following lines?

"main": "./js/all.js",
"types": "./js/all.d.ts",

@juliecki
Copy link
Author

juliecki commented Mar 5, 2025

Adding this to the node_modules/primelocale/package.json

{
    "name": "primelocale",
    "version": "2.0.1",
     …
    "main": "./js/all.js",
    "types": "./js/all.d.ts"
}

and having "moduleResolution": "node" solved the mentioned issue.

Althought, this error now occurs in my jest tests:

 import { ar } from "./ar.js";
    ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      1 | import { i18n } from "@lingui/core";
      2 | import { addLocale, locale } from "primereact/api";
    > 3 | import { all, AllLocales } from "primelocale";
        | ^
      4 |
```

@melloware melloware assigned blutorange and unassigned melloware Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants