.js
/ .mjs
/ .cjs
hidden files can not be imported #42595
Closed
Description
Version
v17.8.0, v16.4.2
Platform
No response
Subsystem
esm
What steps will reproduce the bug?
Let's say we have the following files:
package.json
:
{
"type": "module",
"main": "index.js"
}
index.js
:
import "./js"; // or "./.mjs" or "./.cjs"
.js
/ .mjs
/ .cjs
:
// nothing
Execute Node.js in the same directory with node .
How often does it reproduce? Is there a required condition?
Always
What is the expected behavior?
I think ".js", ".mjs" or ".cjs" should be imported and executed.
Even if this files might traditionally be considered hidden files on some systems and path.extname(".js")
gives the empty string in Node.js, some people may consider these are not extensionless files, but rather files with an explicit extension but no basename.
What do you see instead?
It produces the following error mesage:
In v17.8.0:
node:internal/errors:465
ErrorCaptureStackTrace(err);
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for [...]/.js. Loading extensionless files is not supported inside of "type":"module" package.json contexts. The package.json file[...]/package.json caused this "type":"module" context. Try changing [...]/.js to have a file extension. Note the "bin" field of package.json can point to a file with an extension, for example {"type":"module","bin":{".js":"./.js.js"}}
In v16.4.2:
node:internal/errors:464
ErrorCaptureStackTrace(err);
^
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension "" for [...]/.js
Additional information
Setting directly "main": ".js"
or "main": ".mjs"
in the package file also does not work, whereas setting "main": ".cjs"
actually executes it as a commonJS module anyway.