Skip to content

Importing from a linked local ES modules package using the package name works with “main” property but fails with “module” #39905

@OfirD1

Description

@OfirD1

Version

15.13.0

Platform

Microsoft Windows NT 10.0.19041.0 x64

Subsystem

No response

What steps will reproduce the bug?

To reproduce, do the following:

  1. exporter: A Node.js package which is an ES module that exports something (e.g., using export default).
  2. importer: A Node.js module that tries to import what exporter exports, using import something from 'exporter'.
  3. Use npm link to locally link exporter to importer.

Then:

  • The setup runs successfully if exporter's package.json uses the main property.
  • The setup run fails if exporter's package.json uses the module property.
    • This failure can be "fixed" by using import something from 'exporter/dist/bundle.js', but that's unacceptable.

Example Code

exporter

|- webpack.config.js
|- package.json
|- /src
  |- index.js
|- /dist
  |- bundle.js

webpack.config.js:

import path from "path";
import { fileURLToPath } from "url";

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default {
  mode: "development",     
  entry: "./src/index.js",
  output: {
    filename: "bundle.js",
    path: path.resolve(__dirname, "dist"),
    library: {
      type: "module",
    },
  },
  experiments: {
    outputModule: true,
  },
};

package.json:

{
  "name": "exporter",
  "version": "1.0.0",
  "main": "dist/bundle.js", <-- *** NOTE THIS LINE ***
  "scripts": {
    "build": "webpack"
  },
  "devDependencies": {
    "webpack": "^5.51.1",
    "webpack-cli": "^4.8.0"
  },
  "type": "module"
}

index.js:

function util() {
  return "I'm a util!";
}
export default util;

importer

|- package.json
|- /src
  |- index.js

package.json

{
  "name": "importer",
  "version": "1.0.0",
  "type": "module"
}

index.js

import util from 'exporter';

console.log(util());

Then:

  1. Linking:
cd exporter
⚡  npm link
⚡  cd importer
⚡  npm link exporter
  1. Executing:
⚡  node importer.js 
I'm a util!

However, if exporter's package.json is changed to:

{
  "name": "exporter",
  "version": "1.0.0",
  "module": "dist/bundle.js", <-- *** NOTE THIS LINE ***
  "scripts": {
    "build": "webpack"
  },
  "devDependencies": {
    "webpack": "^5.51.1",
    "webpack-cli": "^4.8.0"
  },
  "type": "module"
}

Then:

  1. Executing:
⚡  node importer.js 

Fails:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'importer\node_modules\exporter\' imported from importer\src\index.js

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

No response

What is the expected behavior?

Node.js should run successfully on both cases

What do you see instead?

Node.js fails to import from a linked package if the latter has the module property set.

Additional information

This was also posted as an SO question, frankly my guess is that's not a bug but I can't be sure.

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