Skip to content

require(dir) ignores exports field in dir/package.json #58827

@sapphi-red

Description

@sapphi-red

Version

22.16.0

Platform

Linux DESKTOP-TJ5RQRK 6.6.87.2-microsoft-standard-WSL2 #1 SMP PREEMPT_DYNAMIC Thu Jun  5 18:30:46 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

module

What steps will reproduce the bug?

Run the script below.

const fs = require('node:fs')

fs.mkdirSync('./test-dir')
fs.writeFileSync('./test-dir/package.json', JSON.stringify({
  name: 'test-dir',
  main: './main.js',
  exports: './exports.js'
}), 'utf-8')
fs.writeFileSync('./test-dir/main.js', 'module.exports = "main"\n')
fs.writeFileSync('./test-dir/exports.js', 'module.exports = "exports"\n')

const result = require('./test-dir')
console.log(`result: ${result}`)
Another case
const fs = require('node:fs')

fs.mkdirSync('./node_modules/test-dir/foo', { recursive: true })
fs.writeFileSync(
  './node_modules/test-dir/foo/package.json',
  JSON.stringify({
    name: 'test-dir',
    main: './main.js',
    exports: './exports.js',
  }),
  'utf-8',
)
fs.writeFileSync(
  './node_modules/test-dir/foo/main.js',
  'module.exports = "main"\n',
)
fs.writeFileSync(
  './node_modules/test-dir/foo/exports.js',
  'module.exports = "exports"\n',
)

const result = require('test-dir/foo')
console.log(`result: ${result}`)

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

Always

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

result: exports is output.

What do you see instead?

result: main is output.

Additional information

The behavior is aligned to what it is written in the docs.

But AFAIK this is the only place where exports field is ignored and main field is used. I wonder if this is an intended behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions