question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

webpack import(`@angular/common/locales/${mylocale}`) not working

See original GitHub issue

Which @angular/* package(s) are the source of the bug?

common

Is this a regression?

Yes

Description

We load our locales dynamically using a dynamic import and registerLocaleData

this works in angular 12

const localeModule = await import(
  /* webpackInclude: /(sv|nb|da|fi|de|fr)/ */
  /* webpackChunkName: "locale" */
  `@angular/common/locales/${this.locale}.js`
)
registerLocaleData(localeModule.default)

The Problem

I found the root cause and it’s not a angular problem, but a webpack one https://github.com/webpack/webpack/issues/13865

But I though someone else might get stuck on the same thing, so I would add it as a regression.

Workaround

A workaround is to use a relative or absolute path to the node_modules folder instead

const localeModule = await import(
  /* webpackInclude: /(sv|nb|da|fi|de|fr).mjs$/ */
  /* webpackExports: "default" */
  /* webpackChunkName: "locale" */
  `node_modules/@angular/common/locales/${this.locale}`
)
registerLocaleData(localeModule.default)

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

Module not found: Error: Package path ./locales is not exported from package /myrepo/node_modules/@angular/common (see exports field in /myrepo/node_modules/@angular/common/package.json)

Please provide the environment you discovered this bug in

No response

Anything else?

No response

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:18
  • Comments:15

github_iconTop GitHub Comments

6reactions
alan-agius4commented, Nov 12, 2021

It’s important to point out that using Webpack specific features such as magic comments above are not supported by the Angular tooling team. The fact that the Angular CLI is using Webpack is an implementation detail which can change in the future.

3reactions
alan-agius4commented, Dec 3, 2021

This is because dynamic imports don’t work in Webpack when an exports with wildcard is specified in package.json

Example

  "exports": {
    "./locales/global/*": {
      "default": "./locales/global/*.js"
    },
    "./locales/*": {
      "default": "./locales/*.mjs"
    },
Read more comments on GitHub >

github_iconTop Results From Across the Web

Angular 13 Cannot find module '@angular/common/locales/fr.js'
So basically I used to load the culture of the user by importing the locale file. const localeUri = `@angular/common/locales/${localeId}.js`; ...
Read more >
Dynamic Import of Locales in Angular | by Michael Karén
Missing locale data for the locale "nb-NO". This is because your module did not import before the pipe tried to use it. Since...
Read more >
Angular Internationalization
Internationalization, sometimes referenced as i18n, is the process of designing and preparing your project for use in different locales around the world.
Read more >
Extending localization - IBM
You can extend the localization support in Sterling Store Engagement to support multiple locales, add languages other than the languages that are supported ......
Read more >
Module Methods - webpack
It is not possible to use a fully dynamic import statement, such as import(foo) . Because foo could potentially be any path to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found