最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

protobuf.js - Angular protobuf injection error after update - Stack Overflow

programmeradmin1浏览0评论

I got an angular application which I wanted to update from version 18 to 19. The update was executed like described in the offical update guide

Within this application im using protobuf.js.

Before the update I was able to import like its mentioned in the offical documentation:

var protobuf = require("protobufjs/minimal");

But after the update I am getting this error:

Uncaught Error: Dynamic require of "protobufjs/minimal" is not supported

While searching for a fix I came up with this solution:

import * as $protobuf from "protobufjs/minimal";

But this produces another error in my main.ts:

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 /errors/NG0203 at injectInjectorOnly (core.mjs:1104:11) at ɵɵinject (core.mjs:1114:40) at ɵɵdirectiveInject (core.mjs:11821:12) at NodeInjectorFactory.AppComponent2_Factory [as factory] (ɵfac.js:1:1) at getNodeInjectable (core.mjs:5315:38) at createRootComponent (core.mjs:17557:31) at ComponentFactory.create (core.mjs:17436:21) at _ApplicationRef.bootstrap (core.mjs:23007:38) at core.mjs:35186:56 at Array.forEach ()

The error occures while executing the following code:

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

One interesting fact. I did not change my AppModule at all. I just updated from Angular 18 to 19. Any ideas on how to fix this?

I got an angular application which I wanted to update from version 18 to 19. The update was executed like described in the offical update guide

Within this application im using protobuf.js.

Before the update I was able to import like its mentioned in the offical documentation:

var protobuf = require("protobufjs/minimal");

But after the update I am getting this error:

Uncaught Error: Dynamic require of "protobufjs/minimal" is not supported

While searching for a fix I came up with this solution:

import * as $protobuf from "protobufjs/minimal";

But this produces another error in my main.ts:

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:1104:11) at ɵɵinject (core.mjs:1114:40) at ɵɵdirectiveInject (core.mjs:11821:12) at NodeInjectorFactory.AppComponent2_Factory [as factory] (ɵfac.js:1:1) at getNodeInjectable (core.mjs:5315:38) at createRootComponent (core.mjs:17557:31) at ComponentFactory.create (core.mjs:17436:21) at _ApplicationRef.bootstrap (core.mjs:23007:38) at core.mjs:35186:56 at Array.forEach ()

The error occures while executing the following code:

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.error(err));

One interesting fact. I did not change my AppModule at all. I just updated from Angular 18 to 19. Any ideas on how to fix this?

Share Improve this question asked Jan 30 at 13:34 Felix GerberFelix Gerber 1,6514 gold badges31 silver badges40 bronze badges 4
  • 1 Did you change builder in angular.json? – Bojan Kogoj Commented Jan 30 at 13:43
  • @bojan-kogoj This was actually a good call. Seems like the angular update automatically change "builder": "@angular-devkit/build-angular:browser" to "builder": "@angular-devkit/build-angular:application". After reverting this, everything works just fine – Felix Gerber Commented Jan 30 at 13:53
  • You likely have a dependency issue, with 2 different core modules getting bundled together. – Matthieu Riegler Commented Jan 30 at 16:16
  • @MatthieuRiegler thanks for your response. Do you have any idea how I could find out which modules trigger this error by beeing bundled together? – Felix Gerber Commented Jan 31 at 9:13
Add a comment  | 

1 Answer 1

Reset to default 1

The comment from @bojan-kogoj directed me into the right direction.

The Update automatically changed my angular.json from

"builder": "@angular-devkit/build-angular:browser" 

to

"builder": "@angular-devkit/build-angular:application"

After setting it back to browser everthing works just fine again

发布评论

评论列表(0)

  1. 暂无评论