Skip to content

Sub folders as modules needs "main" instead of "exports" in package.json  #46344

@kwasimensah

Description

@kwasimensah

Version

v18.9.1

Platform

Darwin 1.6.0 Darwin Kernel Version 21.6.0: Mon Aug 22 20:17:10 PDT 2022; root:xnu-8020.140.49~2/RELEASE_X86_64 x86_64

Subsystem

No response

What steps will reproduce the bug?

in https://github.com/kwasimensah/exports-not-loaded

  1. run node test.js - fails with module not found error
  2. Edit internal/foo/package.json to use "main" instead of "main_ignore"
  3. node test.js now prints "Hello world"

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

Always

What is the expected behavior?

requiring internal/foo should work without specifying main in the package.json per the legacy banner at https://nodejs.org/docs/latest/api/modules.html#folders-as-modules

What do you see instead?

rror: Cannot find module 'internal/foo'
Require stack:
- /Users/kwasimensah/Documents/GitHub/exports-not-loaded/test.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:955:15)
    at Module._load (node:internal/modules/cjs/loader:803:27)
    at Module.require (node:internal/modules/cjs/loader:1021:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at Object.<anonymous> (/Users/kwasimensah/Documents/GitHub/exports-not-loaded/test.js:1:13)
    at Module._compile (node:internal/modules/cjs/loader:1119:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1173:10)
    at Module.load (node:internal/modules/cjs/loader:997:32)
    at Module._load (node:internal/modules/cjs/loader:838:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ '/Users/kwasimensah/Documents/GitHub/exports-not-loaded/test.js' ]
}

Additional information

The regex at

const EXPORTS_PATTERN = /^((?:@[^/\\%]+\/)?[^./\\%][^/\\%]*)(\/.*)?$/;
doesn't deal with subpaths. It's not directly clear why the request needs to be parsed and can't use use path.resolve/path.join

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