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

--experimental-specifier-resolution=node doesn't follow the main field in package.json #38739

Description

@hronro

What steps will reproduce the bug?

project structure:

├── package.json
└── src
   ├── foo
   │  ├── package.json
   │  └── src
   │     └── index.js
   └── index.js
// package.json
{
  "main": "src/index.js",
  "type": "module"
}
// src/foo/package.json
{
  "main": "src/index.js",
  "type": "module"
}
// src/foo/src/index.js
export default 'foo'
// src/index.js
import foo from './foo'

console.log(`hello from ${foo}`)

Run node --experimental-specifier-resolution=node .

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

Always

What is the expected behavior?

Should see hello from foo in the terminal

What do you see instead?

node:internal/process/esm_loader:74
    internalBinding('errors').triggerUncaughtException(
                              ^

Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/xxx/src/foo' imported from /xxx/src/index.js
    at new NodeError (node:internal/errors:363:5)
    at finalizeResolution (node:internal/modules/esm/resolve:296:11)
    at moduleResolve (node:internal/modules/esm/resolve:742:10)
    at Loader.defaultResolve [as _resolve] (node:internal/modules/esm/resolve:853:11)
    at Loader.resolve (node:internal/modules/esm/loader:89:40)
    at Loader.getModuleJob (node:internal/modules/esm/loader:242:28)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:73:40)
    at link (node:internal/modules/esm/module_job:72:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Additional information

Activity

  1. aduh95 commented on May 20, 2021

    @aduh95
    Contributor

    @nodejs/modules

  2. ljharb commented on May 20, 2021

    @ljharb
    SponsorMember

    I believe this is quite intentional; see #36918.

    Especially when using ESM, the intention is to use "exports" and not "main" (altho a published package should ofc provide both, for back compat).

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

    esmIssues and PRs related to the ECMAScript Modules implementation.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions