Skip to content

Fix Node 24 CLI startup by removing JSON module import dependency from version loading#51

Closed
Copilot wants to merge 3 commits intomainfrom
copilot/fix-cli-syntax-error-node24
Closed

Fix Node 24 CLI startup by removing JSON module import dependency from version loading#51
Copilot wants to merge 3 commits intomainfrom
copilot/fix-cli-syntax-error-node24

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 23, 2026

npx dotcontext could fail immediately on Node.js v24 due to JSON import syntax changes (assert { type: 'json' } deprecation path). The failure occurred before command execution in the version/package metadata load path.

  • Runtime compatibility: package metadata loading

    • Reworked src/version.ts to load package.json via fs/path instead of JSON module import semantics.
    • Added a small typed loader with explicit error context if metadata cannot be read/parsed.
    • Added one-time in-module caching to avoid repeated file reads.
  • CLI contract coverage

    • Extended src/cli.test.ts with a focused assertion that --version output equals the exported VERSION constant (the same path used by the CLI).
  • Resulting behavior

    • CLI startup/version resolution no longer depends on Node’s JSON import assertion/attribute parser behavior, eliminating the Node 24 syntax failure mode.
// src/version.ts (updated pattern)
import * as fs from 'fs';
import * as path from 'path';

const packageJsonPath = path.resolve(__dirname, '../package.json');
const packageJsonContent = fs.readFileSync(packageJsonPath, 'utf8');
const pkg = JSON.parse(packageJsonContent) as { version: string; name: string };
Original prompt

This section details on the original issue you should resolve

<issue_title>CLI fails on Node.js v24 with "SyntaxError: Unexpected identifier 'assert'" due to deprecated JSON import assertion</issue_title>
<issue_description>## Describe the bug

Running npx dotcontext on Node.js v24.14.0 fails immediately with a syntax error caused by this line in the CLI entrypoint:

import pkg from '../package.json' assert { type: 'json' };

The error thrown is:

file:///var/home/hayslanleal/.npm/_npx/762a386927734257/node_modules/dotcontext/dist/cli.js:6
import pkg from '../package.json' assert { type: 'json' };
                                  ^^^^^^

SyntaxError: Unexpected identifier 'assert'
    at compileSourceTextModule (node:internal/modules/esm/utils:318:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:99:18)
    at #translate (node:internal/modules/esm/loader:473:20)
    at afterLoad (node:internal/modules/esm/loader:529:29)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:534:12)
    at #getOrCreateModuleJobAfterResolve (node:internal/modules/esm/loader:577:36)
    at afterResolve (node:internal/modules/esm/loader:625:52)
    at ModuleLoader.getOrCreateModuleJob (node:internal/modules/esm/loader:631:12)
    at ModuleJob.syncLink (node:internal/modules/esm/module_job:160:33)
    at ModuleJob.link (node:internal/modules/esm/module_job:245:17)

Node.js v24.14.0
To reproduce
Use Node.js v24.14.0
Run:
npx dotcontext
Expected behavior

The CLI should start normally.

Actual behavior

The CLI crashes before startup with:

SyntaxError: Unexpected identifier 'assert'
Environment
OS: Linux
Node.js: v24.14.0
Invocation method: npx dotcontext
Root cause

It looks like the package is using the old JSON import assertion syntax:

import pkg from '../package.json' assert { type: 'json' };

In newer Node.js versions, JSON module loading uses import attributes with with { type: 'json' } instead of assert { type: 'json' }. This has already caused similar breakages in other tools when running on Node 22+ / 24+.

Suggested fix

Replace:

import pkg from '../package.json' assert { type: 'json' };

With:

import pkg from '../package.json' with { type: 'json' };

Alternatively, to maximize compatibility across runtimes/build tools, the package could avoid static JSON ESM import entirely and read package.json via a Node-compatible fallback such as createRequire() or filesystem access. A common workaround is using createRequire(import.meta.url) for package.json access.

Additional context

Node ecosystem discussions and downstream issues indicate that the assert form for JSON imports is no longer valid in newer Node versions, and projects have been migrating to with { type: 'json' } accordingly.</issue_description>

<agent_instructions>Investigate it and propose a fix</agent_instructions>

## Comments on the Issue (you are @copilot in this section)

<comments>
<comment_new><author>@vinilana</author><body>
Thanks for reporting it! I'll investigate</body></comment_new>
</comments>

🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Copilot AI changed the title [WIP] Fix CLI syntax error on Node.js v24 due to JSON import assertion Fix Node 24 CLI startup by removing JSON module import dependency from version loading Mar 23, 2026
Copilot AI requested a review from vinilana March 23, 2026 18:47
@vinilana vinilana marked this pull request as ready for review April 1, 2026 10:48
@vinilana vinilana closed this Apr 9, 2026
@vinilana vinilana deleted the copilot/fix-cli-syntax-error-node24 branch April 9, 2026 21:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CLI fails on Node.js v24 with "SyntaxError: Unexpected identifier 'assert'" due to deprecated JSON import assertion

2 participants