node icon indicating copy to clipboard operation
node copied to clipboard

`.js` / `.mjs` / `.cjs` hidden files can not be imported

Open PolariTOON opened this issue 2 years ago • 4 comments

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.

PolariTOON avatar Apr 04 '22 00:04 PolariTOON

@nodejs/loaders

aduh95 avatar Apr 04 '22 15:04 aduh95

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?).

guybedford avatar Apr 04 '22 15:04 guybedford

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

GeoffreyBooth avatar Apr 04 '22 16:04 GeoffreyBooth

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 :see_no_evil: .

Hope that helps anyway.

PolariTOON avatar Aug 07 '22 11:08 PolariTOON