Angular tutorial gives error NG0203: inject() must be called from an injection context

Discussão em 'Angular' iniciado por Scott Langham, Fevereiro 5, 2025.

  1. I've followed the Angular hello world tutorial. After running ng serve and opening Chrome to http://localhost:4200 I see a blank page in the browser. Chrome DevTools console shows the error:

    ERROR RuntimeError: NG0203: inject() must be called from an injection context such as a constructor, a factory function, a field initializer, or a function used with `runInInjectionContext`. Find more at https://angular.dev/errors/NG0203
    at injectInjectorOnly (core.mjs:1096:11)
    at ɵɵinject (core.mjs:1106:40)
    at Object.factory (core.mjs:19227:42)


    I've searched the sample app code I've downloaded and do not find any call to inject. What could be wrong?

    I'm using Windows 11 and have the versions installed. Here's some commands I tried.

    ❯ node -v
    v22.13.1

    ❯ ng version

    _ _ ____ _ ___
    / \ _ __ __ _ _ _| | __ _ _ __ / ___| | |_ _|
    / △ \ | '_ \ / _` | | | | |/ _` | '__| | | | | | |
    / ___ \| | | | (_| | |_| | | (_| | | | |___| |___ | |
    /_/ \_\_| |_|\__, |\__,_|_|\__,_|_| \____|_____|___|
    |___/


    Angular CLI: 19.1.5
    Node: 22.13.1
    Package Manager: npm 10.9.2
    OS: win32 x64

    Angular:
    ...

    Package Version
    ------------------------------------------------------
    @angular-devkit/architect 0.1901.5 (cli-only)
    @angular-devkit/core 19.1.5 (cli-only)
    @angular-devkit/schematics 19.1.5 (cli-only)
    @schematics/angular 19.1.5 (cli-only)


    ❯ npm install

    up to date, audited 1126 packages in 3s

    158 packages are looking for funding
    run `npm fund` for details

    10 moderate severity vulnerabilities

    To address issues that do not require attention, run:
    npm audit fix

    To address all issues (including breaking changes), run:
    npm audit fix --force

    Run `npm audit` for details.

    ❯ npm run build

    > angular.dev@0.0.0 build
    > ng build

    Initial chunk files | Names | Raw size | Estimated transfer size
    main-76SWU2S6.js | main | 96.55 kB | 28.60 kB
    polyfills-FFHMD2TL.js | polyfills | 34.52 kB | 11.28 kB
    styles-GSBFIRKP.css | styles | 298 bytes | 298 bytes

    | Initial total | 131.37 kB | 40.18 kB

    Application bundle generation complete. [2.824 seconds]

    Output location: D:\SampleWebApp\first-app_01-hello-world\dist\first-app

     D:  SampleWebApp  first-app_01-hello-world   4.566s  12:40:03
    ❯ npx run start
    Watching D:\SampleWebApp\first-app_01-hello-world and all sub-directories not excluded by your .gitignore. Will not monitor dotfiles.
    Found & ignored ./node_modules ; is listed in .gitignore
    Found & ignored ./dist\first-app\browser\styles-GSBFIRKP.css ; has ignored extension
    Found & ignored ./src\app\app.component.css ; has ignored extension
    Found & ignored ./src\styles.css ; has ignored extension

    Starting: start
    node:internal/modules/cjs/loader:1247
    throw err;
    ^

    Error: Cannot find module 'D:\SampleWebApp\first-app_01-hello-world\start'
    at Function._resolveFilename (node:internal/modules/cjs/loader:1244:15)
    at Function._load (node:internal/modules/cjs/loader:1070:27)
    at TracingChannel.traceSync (node:diagnostics_channel:322:14)
    at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)
    at node:internal/main/run_main_module:36:49 {
    code: 'MODULE_NOT_FOUND',
    requireStack: []
    }

    Node.js v22.13.1

    ❯ ng serve
    Initial chunk files | Names | Raw size
    polyfills.js | polyfills | 90.20 kB |
    main.js | main | 1.33 kB |
    styles.css | styles | 457 bytes |

    | Initial total | 91.98 kB

    Application bundle generation complete. [1.365 seconds]

    Watch mode enabled. Watching for file changes...
    NOTE: Raw file sizes do not reflect development server per-request transformations.
    ➜ Local: http://localhost:4200/
    ➜ press h + enter to show help



    [​IMG]

    Continue reading...

Compartilhe esta Página