Skip to content

cjs format error in [email protected] #2342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 2 tasks
liangskyli opened this issue May 29, 2025 · 2 comments
Open
1 of 2 tasks

cjs format error in [email protected] #2342

liangskyli opened this issue May 29, 2025 · 2 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@liangskyli
Copy link
Contributor

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

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

Extra

@liangskyli liangskyli added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels May 29, 2025
@liangskyli
Copy link
Contributor Author

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
Copy link
Contributor Author

[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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

1 participant