Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.js / .mjs / .cjs hidden files can not be imported #42595

Closed
PolariTOON opened this issue Apr 4, 2022 · 5 comments
Closed

.js / .mjs / .cjs hidden files can not be imported #42595

PolariTOON opened this issue Apr 4, 2022 · 5 comments
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.

Comments

@PolariTOON
Copy link

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.

@VoltrexKeyva VoltrexKeyva added the esm Issues and PRs related to the ECMAScript Modules implementation. label Apr 4, 2022
@aduh95
Copy link
Contributor

aduh95 commented Apr 4, 2022

@nodejs/loaders

@guybedford
Copy link
Contributor

It seems this is primarily because path.extname('.js') === ''. require('./.mjs') or require('./.js') in type module also might not work as expected either (possibly misdetecting and falling back to cjs?).

@GeoffreyBooth
Copy link
Member

What is the use case for supporting a file named .js?

@PolariTOON
Copy link
Author

First, sorry for the late answer.
Not sure it counts as an use case given it's very specific, but here is at least how I came across the issue.

I was making a static site generator with whose default behavior is just to copy the files as is to be served as is.
I needed a way to allow to override the building of any part of the site and at some point I decided that I could put a JS file anywhere able to override any file that shares the same prefix.
You can basically see these JS modules as "decorators" for a set a files.
Example: /foo/.js can rebuild, add or remove any file (except itself) under /foo/ and /foo/bar..js can rebuild, add or remove any file that starts with /foo/bar. (except itself).
Unusual names, I know, but that is intended to let the user free of choosing almost any file name for the assets.

I did not realize until testing that Node.js would not be able to import these "directory decorators", so i hade to switch to file names even more unusual that Node.js see as valid specifiers: /foo/..js and /foo/bar...js 🙈 .

Hope that helps anyway.

@PolariTOON
Copy link
Author

It appears it has been fixed in Node.js 18.19.0 / 20.10.0 / 21.0.0, likely as part of the support for extensionless files added in #49974 . Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
esm Issues and PRs related to the ECMAScript Modules implementation.
Projects
None yet
Development

No branches or pull requests

5 participants