error TS2304: Cannot find name 'localStorage' on Angular app build in NX

Discussão em 'Angular' iniciado por Verrick, Novembro 6, 2024.

  1. Verrick

    Verrick Guest

    I'm not able to run the 'build' command on an Angular app inside a NX workspace anymore. An error occurs while NX is building a dependent library that accesses 'localStorage' on the dom:

    Executing task: npx nx run my-app:build --configuration=production

    × 1/20 dependent project tasks failed (see below)
    √ 19/20 dependent project tasks succeeded [19 read from cache]

    libs\my-lib\src\example-file.ts:43:5 error TS2304: Cannot find name 'localStorage'.


    When I build the library that causes the issue alone (Without the Angular App), i don't get any error:

    Executing task: npx nx run my-lib:build

    √ 8/8 dependent project tasks succeeded [8 read from cache]

    NX Successfully ran target build for project my-lib and 8 tasks it depends on (1s)


    The issue occurs both with 'production' and 'development' builds.

    The issue only occurs with the 'build' command, everything works well with the 'serve' command.

    I tried adding "lib": ["es2022", "dom"] on tsconfig.json to allow access to the DOM.

    I tried adding "ssr": false and "prerender": false to project.json file of the angular app, as I have seen that some people get the issue while trying to access localStorage on Server Side Rendering.

    Any help would be greatly appreciated.

    Angular version: 18.2.10

    NX version: 20.0.7

    Typescript version: 5.5.4

    Continue reading...

Compartilhe esta Página