Is importing by the absolute specifier stable? #43947
-
Hi folks! I'm trying to import CommonJS scripts by the absolute specifier. const fileId = pathToFileURL(path.resolve('rootFile.js')).toString();
const module = require(fileId); That code throws an error:
But I'm sure that it is there and the path is correct. The fun thing is that the following code works just fine: const fileId = pathToFileURL(path.resolve('rootFile.mjs')).toString();
const module = await import(fileId); And the next too: const fileId = path.resolve('rootFile.js');
const module = require(fileId); I'm using MacOS 11.6.7 with Node 16.16.0. Maybe it's worth mentioning that I'm trying to import the file from the other package. Though it's package.json doesn't have neither the What's going on? Is it the bug in NodeJS or I doesn't know something (which is likely)? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Beta Was this translation helpful? Give feedback.
pathToFileURL().toString();
converts the path into url-encoded file protocol string,and node require simply doesn't support using that format to import modules.