-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
process.mainModule is undefined with --experimental-modules #15760
Comments
I've always just checked |
@mscdex the |
I'm really not sure how to support this for ES Modules without having something special like It would be necessary to have it be special because it need to be able to change based on the file importing it. Something similar is required for cc @bmeck |
wait on |
Perhaps we could expose some identifier in |
On a related note, when running node with --experimental-modules flag the const rootPath = path.dirname('.'); // ESM
const rootPath = __dirname; // CommonJS
const favPath = path.dirname('./favicon.ico'); // ESM
const favPath = `${__dirname}/favicon.ico`; // CommonJS |
@danactive I asked about that the other day and it looks like node will support the However I think One criticism I have of // Not browser compatible
import url from "url";
const file = url.resolve(import.meta.url, '../../foo.bar');
// Not Node compatible (as URL isn't global in Node)
const file = new URL('../../foo.bar', import.meta.url).href;
// Also not browser compatible
import url from "url";
const file = new url.URL('../../foo.bar', import.meta.url).href;
// Compatible with both but sort've defeats the point of having
// url.resolve/path.join in the first place
const file = import.meta.url + '/../../foo.bar' |
@danactive I think import.meta is something that will make it's way through the standards body eventually and node soon after (I hope 10 LTS will have it) until then you might want to explore using the --loader flag with a custom loader. I have played around with this and tried many different designs, but all lead to the same question of how to ensure that your modules are consumed correctly downstream. I eventually found this to be a bit of an overkill to manage if all you want is __filename. Long story short, a less elegant but temporary solution that can easily be written in a single line is to use Error().stack with a regex at the top of modules that require such metadata like: //#region META
const FILENAME = typeof __filename !== 'undefined' ? __filename : (/^ +at (?:file:\/*(?=\/)|)(.*?):\d+:\d+$/m.exec(Error().stack) || '')[1];
const DIRNAME = typeof __dirname !== 'undefined' ? __dirname : FILENAME.replace(/[\/\\].*?$/, '');
console.log(process.argv[1], { FILENAME, DIRNAME });
//#endregion @Jamesernator I think that import.meta is actually going to be more like an open singleton per module that may be used to keep runtime populated information in a more open fashion. With a custom loader, it is possible to make a similar mechanism (I can dig out some examples if you need). I think I could get it to work reliably using two different approaches.
In the end, I really recommend waiting for import.meta to settle before taking such risks. Meanwhile, my two cents would be to try to structure your code so that you only need __filename in as few files as possible and use the less elegant solution (which might need very little tweaking for browsers). |
Will |
@matthewp yes |
@devsnek Thanks, this issue focuses on |
@matthewp a cjs imported by an esm has no parent. |
Closing as this is by design. |
Create
index.mjs
file having the contents:Run
node
from command line:There is an entry script detection line in
.js
file that used as an entry pointif (process.mainModule === module) { start(); }
. I'm trying to convert it to.mjs
and I'd like know if my script is being run directly or being imported by another module.The text was updated successfully, but these errors were encountered: