Skip to content

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

@devhayslan

Description

@devhayslan

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.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions