openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

cjs format error in [email protected]

Open liangskyli opened this issue 1 year ago • 4 comments

openapi-typescript version

7.8.0

Node.js version

18.20

OS + version

windows 11

Description

E:\git_code\trade\openapi-ts\node_modules.pnpm\[email protected][email protected]\node_modules\openapi-typescript\dist\lib\redoc.cjs:7\r const parseJson = require('parse-json');\r ^\r \r Error [ERR_REQUIRE_ESM]: require() of ES Module E:\git_code\trade\openapi-ts\node_modules.pnpm\[email protected]\node_modules\parse-json\index.js from E:\git_code\trade\openapi-ts\node_modules.pnpm\[email protected][email protected]\node_modules\openapi-typescript\dist\lib\redoc.cjs not supported.\r

Reproduction

when use "require": "./dist/index.cjs", will see Error [ERR_REQUIRE_ESM] because parse-json not supporting cjs!

Expected result

no Error [ERR_REQUIRE_ESM]

Required

  • [x] My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

liangskyli avatar May 29 '25 03:05 liangskyli

run: pnpm vitest run test/cjs.test.js Unit testing in the project did not pass,

E:\git_code\liangskyli\openapi-typescript\packages\openapi-typescript>pnpm vitest run test/cjs.test.js

 RUN  v3.1.4 E:/git_code/liangskyli/openapi-typescript/packages/openapi-typescript


⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Failed Suites 1 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯

 FAIL  test/cjs.test.js [ test/cjs.test.js ]
Error: require() of ES Module E:\git_code\liangskyli\openapi-typescript\node_modules\.pnpm\[email protected]\node_modules\parse-json\index.js from E:\git_code\liangskyli\openapi-typescript\packages\openapi-typescript\dist\lib\redoc.cjs not supported.
Instead change the require of index.js in E:\git_code\liangskyli\openapi-typescript\packages\openapi-typescript\dist\lib\redoc.cjs to a dynamic import() which is available in all CommonJS modules.
 ❯ Object.<anonymous> dist/lib/redoc.cjs:7:19
 ❯ Object.<anonymous> dist/index.cjs:7:15
 ❯ test/cjs.test.js:3:45
      1| // important: MUST use require()!
      2| const { fileURLToPath } = require("node:url");
      3| const { astToString, default: openapiTS } = require("../dist/index.cjs");
       |                                             ^
      4|
      5| const TIMEOUT = 10_000;

⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯[1/1]⎯


 Test Files  1 failed (1)
      Tests  no tests
   Start at  15:18:26
   Duration  715ms (transform 28ms, setup 0ms, collect 0ms, tests 0ms, environment 0ms, prepare 137ms)

liangskyli avatar May 29 '25 07:05 liangskyli

[email protected] is ok, build file:

// src/lib/redoc.ts
var import_openapi_core = require("@redocly/openapi-core");
var import_node_perf_hooks = require("node:perf_hooks");
var import_node_stream = require("node:stream");
var import_node_url = require("node:url");

// ../../node_modules/.pnpm/[email protected]/node_modules/parse-json/index.js
var import_code_frame = __toESM(require_lib2(), 1);

errror in #2310 use unbuild build cjs, I did not find that Unbuild can be configured to adapt to builds that only support ES packages

build file:

'use strict';

const openapiCore = require('@redocly/openapi-core');
const node_perf_hooks = require('node:perf_hooks');
const node_stream = require('node:stream');
const node_url = require('node:url');
const parseJson = require('parse-json');
const utils = require('./utils.cjs');

const parseJson = require('parse-json'); ----- build error in here

liangskyli avatar May 29 '25 09:05 liangskyli

@drwpow : #2310 not support cjs, see unbuild bug I haven't found a better way to support CJS. Should we roll back this submit?(#2310)

liangskyli avatar Aug 02 '25 13:08 liangskyli

Since openapi-typescript is now in maintenance mode, this is fixed in openapi-typesafe, a maintained fork of openapi-typescript, openapi-fetch and openapi-react-query: CommonJS projects get the types and a working require() (the exports carry types conditions for require and import). Apart from fixes like this one, the CLI flags, Node API and output match openapi-typescript 7.13, and npx openapi-typesafe-migrate --write moves a project over. https://github.com/openapi-typesafe/openapi-typesafe

The 1.0 release candidate is on npm: npm i -D openapi-typesafe@next.

fedorello avatar Nov 04 '26 18:11 fedorello