'A locally build NPM package - generated by the openapi-generator - does not seem to export the API, although it's available
I created a REST service interface definition with one very simple GET endpoint using OpenAPI 3.
(There is a minified version of that interface in the spoiler.)
{"openapi":"3.0.3","info":{"title":"Identity Cache API Definition","version":"1.0"},"tags":[{"name":"identity-cache"}],"paths":{"/{identityId}/data":{"get":{"tags":["identity-cache"],"summary":"Returns data.","operationId":"getIdentityData","parameters":[{"name":"identityId","in":"path","description":"The identity ID.","required":true,"schema":{"type":"string"}}],"responses":{"200":{"description":"The identity data.","content":{"application/json":{"schema":{"$ref":"#/components/schemas/IdentityData"}}}}}}}},"components":{"schemas":{"IdentityData":{"type":"object","required":["identityId"],"properties":{"identityId":{"type":"string"}}}}}}
Then I generated the typescript-fetch source code with the openapi-generator:
openapi-generator-cli generate -i ../identity-cache-api.yaml -o ./ -g typescript-fetch \
--additional-properties=supportsES6=true,typescriptThreePlus=true,npmVersion=1.0.0,npmName=identity-cache-fetch
After that I built the generated npm package with Node.js v14.7.0 using npm:
npm run build
And finally I added the built package locally to my project with npm install. Here is the added line in the package.json:
"identity-cache-fetch": "file:../identity-cache-npm-package",
Seems good, Visual Studio Code recognizes the new package and let me use it:
import { Configuration, IdentityCacheApi } from "identity-cache-fetch";
const identityCacheApi = new IdentityCacheApi(new Configuration({ basePath: import.meta.env.VITE_BACKEND_URL }));
However the build fails with: vite build
This error arises:
[INFO] error during build:
[INFO] Error: 'IdentityCacheApi' is not exported by ../identity-cache-npm-package/dist/index.js, imported by src/services/service.ts
[INFO] at error (../frontend/node_modules/rollup/dist/shared/rollup.js:160:30)
[INFO] at Module.error (../frontend/node_modules/rollup/dist/shared/rollup.js:12438:16)
[INFO] at Module.traceVariable (../frontend/node_modules/rollup/dist/shared/rollup.js:12808:29)
[INFO] at ModuleScope.findVariable (../frontend/node_modules/rollup/dist/shared/rollup.js:11601:39)
[INFO] at Identifier.bind (../frontend/node_modules/rollup/dist/shared/rollup.js:6479:40)
[INFO] at NewExpression.bind (../frontend/node_modules/rollup/dist/shared/rollup.js:5087:23)
[INFO] at VariableDeclarator.bind (../frontend/node_modules/rollup/dist/shared/rollup.js:5087:23)
[INFO] at VariableDeclaration.bind (../frontend/node_modules/rollup/dist/shared/rollup.js:5083:31)
[INFO] at Program.bind (../frontend/node_modules/rollup/dist/shared/rollup.js:5083:31)
[INFO] at Module.bindReferences (../frontend/node_modules/rollup/dist/shared/rollup.js:12434:18)
[INFO] npm ERR! code ELIFECYCLE
[INFO] npm ERR! errno 1
The index.js seems good. For simplicity, I'm only posting the TypeScript code of the uncompiled index.ts.
/* tslint:disable */
/* eslint-disable */
export * from './runtime';
export * from './apis';
export * from './models';
What am I missing?
Or is it a bug in the openapi-generator or vite?
Solution 1:[1]
It's an rollup issue.
By adding an ESM (ECMAScript Module) to the generated code the project finally builds.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Sven Döring |
