-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
NX Plugin throws when importing ESM (even with workaround) #18974
Comments
I've dug deeper into this issue, and found out the cause is a known issue in v8-compile-cache (zertosh/v8-compile-cache#41). Nx v8-compile-cache uses when it detects the workspace is not using angular, by trying to load @angular/cli. Line 55 in 7508e7b
I believe that dependency is really necessary, but it would be nice to have a flag to turn it off as a workaround for this case. |
There is a env flag to disable v8-compile-cache: I've updated the workaround from #15682 to include a warning about this: /**
* Workaround for Import ESM modules in plugin
*
* @see {@link https://github.com/nrwl/nx/issues/18974} related issue
* @see {@link https://github.com/nrwl/nx/issues/15682} related issue
* @see {@link https://gist.github.com/passbyval/b85f79381816c197c5c651b7c0b00d5e} for alternative approach
*
* @example
* const { default: chalk } = await requireEsm<typeof import('chalk')>('chalk');
*/
export async function requireEsm<T>(module: string) {
try {
return await (Function(`return import("${module}")`)() as Promise<T>);
} catch (e) {
if (e instanceof TypeError && 'code' in e && e.code === 'ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING') {
console.error([
'Nx failed to import ESM module due to https://github.com/nrwl/nx/issues/18974',
'Add DISABLE_V8_COMPILE_CACHE=1 to your .env'
].join('\n'));
}
throw e;
}
} |
Hey Julio! I'm attaching a quick fix for generators + format that will close this issue out. Plugins still won't work if they try to import ESM during the project graph creation process, but generators, executors etc should be compatible with it now. |
Awesome, thanks! I believe the issue will still happen with custom executors, but the user-side workaround with |
It shouldn't hit custom executors since they are in a different process which doesn't hit the v8 compile cache, hence why vitest was always working |
The fix didn't work for me, as documented in #18721. The work-around does work. |
This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context. |
Current Behavior
There is a known issue #15682 on using ESM dependencies in NX Plugins, which requires the following workaround:
The problem is this won't work for some dependencies (ie chalk@5), with
node
throwing on the module import with eitherA dynamic import callback was not specified.
orInvalid host defined options
errors.I've tried reproducing this using nx-examples repository, but initially it worked fine there. After a lot of search comparing that repository with my own, I found out the issue only happens when
@angular/cli@~16.2.0
is not installed as a dependency (!!!)I was unable to find anything further on the issue, but it looks like there is some code in
nx
that checks for that dependency, by callingrequire.resolve
on it. I'm not sure if that might cause any side effect (ie running the dependency code somehow?) or if nx changes behavior based on that...Expected Behavior
ESM should be able to load, even if the workaround for #15682 is required.
GitHub Repo
https://github.com/JulioC/nx-examples/tree/bug/esm-import-in-plugins
Steps to Reproduce
bug/esm-import-in-plugins
)yarn nx g @nx-example/nx-workspace-plugin:example
to see the issueNx Report
Failure Logs
Package Manager Version
No response
Operating System
Additional Information
ts-node
or@swc-node/register and @swc/core
for typescript executionThe text was updated successfully, but these errors were encountered: