镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Customization hooks / ESM loaders reports importing JSON warning on commonjs modules #53318

Description

@hyrious

Version

v22.1.0

Platform

Darwin MAGIT00999.local 23.5.0 Darwin Kernel Version 23.5.0: Wed May 1 20:09:52 PDT 2024; root:xnu-10063.121.3~5/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

Prepare the 3 files:

// register.mjs
import { register } from 'module'
import { readFile } from 'fs/promises'

export async function load(url, context, nextLoad) {
  const result = await nextLoad(url, context)
  if (result.format === 'commonjs') {
    // https://nodejs.org/api/module.html#:~:text=result.source%20%3F%3F%3D%20await%20readFile(new%20URL(result.responseURL%20%3F%3F%20url))%3B
    result.source ??= await readFile(new URL(result.responseURL ?? url))
  }
  return result
}

register('./register.mjs?' + Date.now(), import.meta.url)

// test.js
require('./package.json')

// package.json
{}

Then execute:

> node --import ./register.mjs test.js
(node:5296) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)

How often does it reproduce? Is there a required condition?

It happens only with:

result.source ??= await readFile(new URL(result.responseURL ?? url));

If it does not handle commonjs files like this, it won't happen.

What is the expected behavior? Why is that the expected behavior?

require(json_file) in commonjs modules should not emit this warning.

What do you see instead?

It emits warning

(node:5296) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time

Additional information

No response

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleIssues and PRs marked stale due to inactivity and scheduled for automatic closure.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions