-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
Utility method for exposing Node’s type resolution #49446
Comments
i guess this is really more like a |
Well first, can For this particular use case, sure, knowing whether or not But that’s not as useful as just knowing whether Node would treat a particular |
Also @devsnek please see eslint/eslint#12333 (comment) for a pretty extensive list of gotchas that one user ran into when trying to replace |
after all is said and done we should be able to load json and native add-ons with import too. the process of migration is always going to be tough. |
I’m confused... I need to load a |
Yes, |
Duplicate of #30514 |
I don't think so. The other issue was specific to the context of loaders, where the needs are met; but the issue here is more general, and covers user application code (like a build tool needing to know how to load a file). The |
Jest would very much use such a function, and I'd love to not have to implement the logic manually. We implement our own |
@SimenB I believe what you need is not to figure out whether it's esm or cjs, but rather what does node's module resolution think the file is, according to its module resolution algorithm. For example, let's say you have a test.js file that has no imports and is in a folder with a package.json with type:module. What node would do is treat it as esm. Even if it has a "require", node would treat it as esm. And what we would expect from jest is to treat it as esm and "import" it and not "require" (or the equivalent you will use with "vm") I added esm support for Mocha, and the trick I used to figure that out was to require the module, and if I got an error from node that says that it's an esm, then I import it. A hack, but given that there is currently no way to figure it out (besides duplicating nodes algorithm), then that is the only solution I could find. |
Why not do it the other way around? |
Not who you asked but I assume because it would break synchronous consumers that may not support async test suite setup. Starting with require means the change in behavior only affects people who actively use modules. P.S.: But I agree that in the general case starting with ESM will be safer, especially in a future where loaders may make it impossible ("harder than reasonably implementable") for CJS to fail when ESM is |
Yeah, I'm hoping the function discussed in the OP will allow us to not have to implement guessing or fallbacks. The use case of loading config presented in the OP is not my main use case, but rather custom implementation of |
@GeoffreyBooth - @jkrems should have been correct, but since we have two paths currently in Mocha—an async one that supports both esm and cjs, and a sync one that supports only cjs and is there for api backward compatibility, then that wasn't a problem for us. No, the real reason is that I was chicken. 😀Mocha is used by millions of tests around the world, and just thinking that suddenly they're all going to pass through the newly deployed |
The |
@GeoffreyBooth look into my esm-loader implamentation to load ESM from string https://github.com/direktspeed/esm-loader |
@GeoffreyBooth and by the way to get the needed informations you can use the new added esm loader hooks they deliver all the information like module resolve algos, https://nodejs.org/api/esm.html#esm_experimental_loaders there are some new added hooks |
@coreyfarrell - this was true up to Node 13.3. From Node 13.4 the warning was removed. This I verified experimentally... |
@coreyfarrell I may have confused two warnings. I wasn't talking about the warning about ESM being experimental, but rather of this warning:
This warning is very problematic for Mocha, and happily was was removed in Node.js 13.4. |
I've just published get-package-type v0.1.0 for this purpose. I need this functionality for nyc to support loading This package was implemented from scratch since the node.js implementation is in C++ so I was unable to trace out exactly what that does. I've opened a couple issues seeking feedback to things that I think need to be resolved before I can bump to v1.0.0. I'd appreciate any comments on those issues. I've also opened cfware/get-package-type#6 for any general comments/discussion as I don't want to take over this thread. |
This issue would have to deal w/ CLI arguments potentially from #37848 |
There has been no activity on this feature request for 5 months and it is unlikely to be implemented. It will be closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document. |
I still definitely want this, but the implementation baked into |
@SimenB @GeoffreyBooth as the issue got closed: see: eslint/eslint#12333 (comment) but without a result i want to supply some impotent internal insights.
High Level PictureThe Browser Platform Manages the life cycle of the isolates that loaded the modules eg: workers lifecycle design which inherent Saw the resolution part to late like the experimental flag for auto module typesparsing the file for import as static syntax and export as static syntax is the only way to say if something is a module or not // is a module if it contains import or export keyword not followed by ( as next char to it no matter how much spaces or new lines are inbetween https://nodejs.org/dist/latest-v21.x/docs/api/cli.html#--experimental-detect-module the |
There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the
never-stale
|
There has been no activity on this feature request and it is being closed. If you feel closing this issue is not the right thing to do, please leave a comment. For more information on how the project manages feature requests, please consult the feature request management document. |
Split off from nodejs/modules#389 (comment). I think it would be useful to have a function that returns how Node would try to interpret a file, in particular whether Node would try to parse a path to a
.js
file as CommonJS or as ESM. This would spare tools from needing to reimplement Node’s “find the nearest parentpackage.json
and see if it has a"type"
field” algorithm.Whether the file would be interpreted successfully as that type is irrelevant; it could be a zero-byte file for all this API cares. One use case for this is for tools loading
.js
config files to know whether to try to load the files viarequire
orimport()
.The text was updated successfully, but these errors were encountered: