Encountering a problem with consuming OpenApi Generated files (services, interfaces) via NPM package. Strangely, it works outside the node_modules folder but ApiModule is undefined when placed within. Refer to the Github link below for documentation on usage.
While the project builds successfully and detects the files in the path, an error occurs upon website launch:
app.module.ts:30 Uncaught TypeError: Cannot read properties of undefined (reading 'forRoot')
at Module.6747 (app.module.ts:30)
at __webpack_require__ (bootstrap:19)
at Module.4431 (environment.ts:16)
at __webpack_require__ (bootstrap:19)
at __webpack_exec__ (log$:23)
at log$:23
at Function.__webpack_require__.O (chunk loaded:23)
at log$:23
at webpackJsonpCallback (jsonp chunk loading:33)
at main.js:1
AppModule:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
////// added
import { HttpClientModule } from '@angular/common/http';
import {
ApiModule,
Configuration,
ConfigurationParameters,
} from 'client-petstore';
export function apiConfigFactory(): Configuration {
const params: ConfigurationParameters = {
basePath: 'https://localhost:4200',
};
return new Configuration(params);
}
////// end
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
////// added
ApiModule.forRoot(apiConfigFactory),
HttpClientModule
////// end
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
Testing this on 2 different machines running Angular 12 (Node.js v12 and v16) with:
My locally generated file;
Downloaded “npm typescript-codegen-petstore” (need to fix some issues);
https://github.com/OpenAPITools/openapi-generator/tree/master/samples/client/petstore/typescript-angular-v12-provided-in-root/builds/with-npm ( for download)
The issue seems to be related to the folder placement – it works outside node_modules but not inside. Any suggestions or assistance would be greatly appreciated. Thank you.