DEV Community

quincarter
quincarter

Posted on

Nx 17 Upgrade Errors - Cannot Use Import outside a Module

Upgrading Nx

The team over at Nx has made it super easy to upgrade your monorepo with a single line command that stages any package migrations for you, and let's you see all changes on a working tree. This approach is very nice if you are in a larger project and want to upgrade 1 or 2 major versions (my project was on Nx 15 and I moved to Nx 17).

In order to upgrade your monorepo, you can run this command:

nx migrate <version>
Enter fullscreen mode Exit fullscreen mode

Or if you want to migrate all the way to latest (at your own risk) then you can just throw your hands up and run this command:

nx migrate latest
Enter fullscreen mode Exit fullscreen mode

Once that is done, it will show you a message kind of like this:

Migration success

Then you will want to rerun yarn or npm i or whatever you prefer to install the latest packages, then run

nx migrate --run-migrations
Enter fullscreen mode Exit fullscreen mode

If everything goes to their plan, your migration will be successful and you can move on!
Image description

If you have any sort of local generators configured via plug-ins with Nx, then read on.

Cannot use import outside a module

Error received when running workspace-generators

Ah so this error is one that I dealt with for quite a while. There's a few github issues for this, but the one I participated in the most was this one.

The Problem

There is a package that gets updated with the nrwl/nx migrations called @swc-node/register any version in the 1.5.x range and the 1.6.x range breaks something inside of the nx internals that I haven't quite figured out why it happened. 1.5.x throws a completely different error than 1.6.x and 1.6.x just hard fails with the error above, 1.5.x throws an error like this:
@swc-node/register error on 1.5.x

The Solution (As of right now October 25, 2023)

Install an earlier version of @swc-node/register to make their generators work (at the time of writing this article).

With Yarn:

yarn add -D @swc-node/register@1.4.2 -W
Enter fullscreen mode Exit fullscreen mode

With Npm:

npm i -D @swc-node/register@1.4.2
Enter fullscreen mode Exit fullscreen mode

Note:

Installing @swc-node/register@1.4.2 with NPM requires --force because of peerDependency conflicts:

See here for more.

myngapp git:(nx-16-10-0) βœ— npm i --force
npm WARN using --force Recommended protections disabled.
npm WARN ERESOLVE overriding peer dependency
npm WARN While resolving: nx@16.10.0
npm WARN Found: @swc-node/register@1.4.2
npm WARN node_modules/@swc-node/register
npm WARN   dev @swc-node/register@"1.4.2" from the root project
npm WARN 
npm WARN Could not resolve dependency:
npm WARN peerOptional @swc-node/register@"^1.6.7" from nx@16.10.0
npm WARN node_modules/nx
npm WARN   dev nx@"16.10.0" from the root project
npm WARN   3 more (@nx/devkit, @nx/workspace, @nrwl/tao)
npm WARN 
npm WARN Conflicting peer dependency: @swc-node/register@1.6.8
npm WARN node_modules/@swc-node/register
npm WARN   peerOptional @swc-node/register@"^1.6.7" from nx@16.10.0
npm WARN   node_modules/nx
npm WARN     dev nx@"16.10.0" from the root project
npm WARN     3 more (@nx/devkit, @nx/workspace, @nrwl/tao)
Enter fullscreen mode Exit fullscreen mode

Run your generators and you should be greeted with a happy response now.

Nx Generator Success

Top comments (0)