nx: nx Typescript generator "Cannot use import statement outside a module"
Documentation issue
- Reporting a typo
- [ X ] Reporting a documentation bug
- Documentation improvement
- Documentation feedback
Is there a specific documentation page you are reporting?
https://nx.dev/extending-nx/recipes/local-generators
Additional context or description
Run the commands as specified in the page and it just doesn’t work when you try to execute the generator.
nx generate @myorg/my-plugin:db-migration mylib
src/generators/db-migration/generator.ts:1
import {
^^^^^^
SyntaxError: Cannot use import statement outside a module
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1176:20)
at Module._compile (node:internal/modules/cjs/loader:1218:27)
at Module._compile (/Users/myuser/IdeaProjects/<proj folder>/node_modules/pirates/lib/index.js:117:24)
at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
at Object.newLoader [as .ts] (/Users/myuser/IdeaProjects/<proj folder>/node_modules/pirates/lib/index.js:121:7)
at Module.load (node:internal/modules/cjs/loader:1117:32)
at Function.Module._load (node:internal/modules/cjs/loader:958:12)
at Module.require (node:internal/modules/cjs/loader:1141:19)
at Module.Mod.require (/Users/myuser/IdeaProjects/<proj folder>/node_modules/nx/bin/init-local.js:207:36)
No matter what I do regarding the package json, tsconfig, project.json, or whatever will make this error go away.
✗ node -v
v18.14.2
✗ npm -v
9.5.0
✗ nx --version
Nx Version:
- Local: v16.9.0
- Global: v16.10.0
Also upgraded everything to 16.10.0 and it still fails to execute.
I understand this is an issue due to the top minds at Node really wanting to emphasize a Python 2/3-like split and have things break. Is my only option to downgrade to node 16 as the docs have a callout specifically for node16? Considering it is now EOL this past September that doesn’t seem like the right solution.
Nx uses the paths from tsconfig.base.json when running plugins locally, but uses the recommended tsconfig for node 16 for other compiler options. See https://github.com/tsconfig/bases/blob/main/bases/node16.json
About this issue
- Original URL
- State: open
- Created 9 months ago
- Reactions: 7
- Comments: 37 (7 by maintainers)
I’m still working on formulating the exact fix we want to go with here, I’m personally leaning towards dropping @swc-node/register as a supported transpiler. Its not that much faster than ts-node, and this isn’t the first time we’ve hit an issue with it.
It took me some time to figure it out:
tsconfig.base.tshas afilespropertyI created a repo with three branches illustrating the different behaviors: https://github.com/rosahealth/nx-17-local-executor-tsconfig-bug/branches/all
The command to run is
npx nx run myngapp:bug-local-executor --skip-nx-cacheOn 16.8.1, it succeeds. While 16.10 and 17 have the following error:Commit from the Nx 17 that makes it crash: https://github.com/rosahealth/nx-17-local-executor-tsconfig-bug/commit/c15ca6889c4b1e64b5ec8933bb413db4c4fa4b34
The content of the
filesarray does not matter. What matters is having the property defined.@AgentEnder Let me know if I can help further on this topic.
This version of @swc-node/register is not compatible with nx 18 anymore. https://github.com/nrwl/nx/blob/master/package.json#L107 A real fix here would be highly appreciated.
We were able to upgrade by moving
tsconfig.base.json"files"into only the tsconfigs that need it. Which we probably should have had it that way to start, but I was lazy 😄i wrote up a quick article outlining this on dev.to if you all would like to read it. But it just basically has my findings i listed in these comments. Share this with others if this ends up being a fix that works for people.
https://dev.to/quincarter/nx-17-upgrade-errors-cannot-use-import-outside-a-module-1ea8
@joeldbirch Make a git commit hook which enforces the version.
@quincarter thanks for the suggestion, but we haven’t manually run that and the package.json change is still constantly sneaking back into our commits.
My team has been trying to workaround this for a while now. We’ve tried setting the version of @swc-node/register to
"1.4.2"as per this comment which works but it seems like NX (or … something) keeps editing the package.json to point back to"^1.6.7". Our team hasn’t pin-pointed what causes this. Does anyone have an idea as to why/how this could happen please?Looking into this more. Can anyone provide the use case for having
files/includesin tsconfig.base.json? I can see how that is causing the problem in newer versions of swc-node.You can set
NX_PREFER_TS_NODEto tell Nx to use it instead, which should avoid this altogether while we work on it.@AgentEnder - i am not sure if there are any vulnerabilities in the
@swc-node/register@1.4.2package, or what the risks are for downgrading it, but That does seem to work and it looks like all of my commands that are nx related seem to be working right now in my project. Not sure if others can confirm here. Maybe @Sieabah or @antoinepairet can confirm?A reproduction would certainly help, thanks for the offer.
yep i am having this same issue going from 15 to 16.x.x or even beyond to 17.x.x
package manager
yarn 1.x.xNode Version
Fails no matter what node version i am on.
it had an error on the initial migration but i reran it and it passed
Failed.generate command
feel free to fork this (it’s in gitlab, not github) and you can try to replicate it. Component Library Starter node version 16.16.x or above run the nx migrate commands to go to latest or 16.x.x I am also running yarn locally
npm script to execute the generator is: