Nx 17 Upgrade Errors - Cannot Use Import outside a Module
Tags: nx, typescript, library, nrwl
Published: 2023-10-26 02:20:34

## 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:

```sh
nx migrate <version>
```

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:

```sh
nx migrate latest
```

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

![Migration success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c66baefl2rwxcbcvws0x.png)

Then you will want to rerun `yarn` or `npm i` or whatever you prefer to install the latest packages, then run
```sh
nx migrate --run-migrations
```

If everything goes to their plan, your migration will be successful and you can move on!
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ribzwnxw04qyli5o76vs.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4zo33af6uvb7txwoqsp7.png)

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](https://github.com/nrwl/nx/issues/19519). 

### 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x84igrfnlmdwgikq7nih.png)

### 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:
```sh
yarn add -D @swc-node/register@1.4.2 -W
```

With Npm:
```sh
npm i -D @swc-node/register@1.4.2
```

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

[See here](https://github.com/nrwl/nx/issues/19519#issuecomment-1780602628) for more. 

```sh
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)
```

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

![Nx Generator Success](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ct0g0rtq9q41h031gqxz.png)