Angular build says it cannot resolve '@angular/localize/init'
Module not found: Error: Can't resolve '@angular/localize/init'Short answer
The build imports @angular/localize, and that package is not in node_modules. Either it was never added to the project, or it is a devDependency and your Docker or CI install skipped dev packages. Add it with ng add, or install with dev dependencies for the build step.
Fix it fast
1Find out which cause you have
Check whether the package is declared, and where.
grep -n "@angular/localize" package.json ls node_modules/@angular/localize/package.json2Match what you see, then jump to its fix
- 1
grep prints nothing, and the import of '@angular/localize/init' or a $localize call is in your code or a library you addedThe package was never added to the projectFix - 2
Works on your machine, fails in a Docker build or CI where npm ci --omit=dev or NODE_ENV=production is setIt is a devDependency and the production install left it outFix
- 1
What does the error look like?
Module not found: Error: Can't resolve '@angular/localize/init'Can't resolve '@angular/localize/init' in '/usr/src/app/src'
Cause 1 of 2
The package was never added to the project
1Check
Search the source for the import or for tagged $localize strings.
grep -rn "localize" src/ | head2Fix
Add it with the CLI. The schematic updates package.json and the TypeScript config files, and puts a type reference at the top of main.ts.
ng add @angular/localize3Verify
The package is listed, and the build gets past the import.
npm ls @angular/localize
npx ng buildConfirmed by angular.dev
Cause 2 of 2
It is a devDependency and the production install left it out
1Check
ng add saves the package in devDependencies by default. npm leaves devDependencies out when omit=dev or when NODE_ENV is production.
grep -n -B1 -A1 "@angular/localize" package.json
grep -n -E "omit|NODE_ENV|production" Dockerfile2Fix
Run the Angular build in a stage that installs dev dependencies, and copy only the built output into the final image. If the app needs $localize at runtime, add the package with the runtime option so it lands in dependencies.
# build stage: dev dependencies installed
RUN npm ci
RUN npx ng build
# or, when $localize is needed at runtime
ng add @angular/localize --use-at-runtime3Verify
Rebuild the image and confirm the build step no longer reports the module.
docker build --progress=plain .Confirmed by docs.npmjs.com
How do I stop it coming back?
- Build Angular in a stage that installs all dependencies, and ship only the output.
- Run ng add for Angular packages so config and types are set in one step.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
Related fixes
Back end
ng serve says it cannot determine the project or target
Cannot determine project for command.
The Angular CLI looked through angular.json for a project that defines the target you asked for (serve, build, test) and found none, or found several and could not pick one.
3 causes, each with a fixBack end
ng fails with Cannot find module @angular/cli/bin/ng
Cannot find module '@angular/cli/bin/ng'
The ng command is the entry point to the Angular CLI package.
2 causes, each with a fixBack end
Angular app shows 'Cannot GET /' when served by a Node.js server
Cannot GET /dashboard
Cannot GET / is Express saying no route or file matches the request.
3 causes, each with a fixBack end
ng serve cannot find the dev-server builder package
Could not find the implementation for builder @angular/build:dev-server
The builder named in angular.json (for example @angular-devkit/build-angular:dev-server) is looked up as an npm package starting from the workspace root.
3 causes, each with a fixBack end
The command npm run build -- --prod exited with code 1 (Angular)
The command "npm run build -- --prod" exited with code 1
Exit code 1 only says the build failed; the real message is above it.
2 causes, each with a fixBack end
Angular build fails with SassError: Undefined variable
SassError: Undefined variable.
Sass modules loaded with @use are scoped to the file that loads them and their variables need a namespace prefix.
4 causes, each with a fix