-
-
Notifications
You must be signed in to change notification settings - Fork 35.2k
Importing from a linked local ES modules package using the package name works with “main” property but fails with “module” #39905
Copy link
Copy link
Closed
Description
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:
exporter: A Node.js package which is an ES module thatexports something (e.g., usingexport default).importer: A Node.js module that tries toimportwhatexporterexports, usingimport something from 'exporter'.- Use
npm linkto locally linkexportertoimporter.
Then:
- The setup runs successfully if
exporter'spackage.jsonuses themainproperty. - The setup run fails if
exporter'spackage.jsonuses themoduleproperty.- This failure can be "fixed" by using
import something from 'exporter/dist/bundle.js', but that's unacceptable.
- This failure can be "fixed" by using
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:
- Linking:
⚡ cd exporter
⚡ npm link
⚡ cd importer
⚡ npm link exporter- 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:
- Executing:
⚡ node importer.js Fails:
Error [ERR_MODULE_NOT_FOUND]: Cannot find package 'importer\node_modules\exporter\' imported from importer\src\index.jsHow 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.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels