nx icon indicating copy to clipboard operation
nx copied to clipboard

While building all errors are sent through a JSON Parser even when they are not JSON in NX 18 with next,

Open kartikye opened this issue 1 year ago • 10 comments

Current Behavior

Failing builds error out without any reporting of what caused the failure

Expected Behavior

It should display the errors and not throw an error attempting to parse them. I downgraded to NX 17 and everything works as expected.

GitHub Repo

No response

Steps to Reproduce

  1. Have a NX monorepo with an nx/next app
  2. Have a bug like a type error or scss error
  3. Attempt to build

Nx Report

NX   Report complete - copy this into the issue template

Node   : 20.11.1
OS     : win32-x64
npm    : 8.19.3

nx (global)        : 16.3.2
nx                 : 18.0.8
@nx/js             : 18.0.8
@nx/jest           : 18.0.8
@nx/linter         : 18.0.8
@nx/eslint         : 18.0.8
@nx/workspace      : 18.0.8
@nx/devkit         : 18.0.8
@nx/eslint-plugin  : 18.0.8
@nx/next           : 18.0.8
@nx/react          : 18.0.8
@nrwl/tao          : 18.0.8
@nx/web            : 18.0.8
typescript         : 5.3.3

Failure Logs

> nx run site:build:production

   ▲ Next.js 14.1.3

   Skipping linting
 ✓ Checking validity of types    
   Creating an optimized production build ...
undefined:1
<w> [webpack.cache.PackFileCacheStrategy] Skipped not serializable cache item 'Compilation/modules|C:\xxx\node_modules\next\dist\build\webpack\loaders\css-loader\src\index.js??ruleSet[1].rules[6].oneOf[9].u
se[0]!C:\xxx\node_modules\next\dist\build\webpack\loaders\postcss-loader\src\index.j
s??ruleSet[1].rules[6].oneOf[9].use[1]!C:\Users\karti\Documents\dots\dots\frontends\node_modules\next\dist\build\webpack\l
oaders\resolve-url-loader\index.js??ruleSet[1].rules[6].oneOf[9].use[2]!C:\xxx\node_
modules\next\dist\compiled\sass-loader\cjs.js??ruleSet[1].rules[6].oneOf[9].use[3]!C:\xxx\apps\site\components\text-hero\text-hero.module.scss': No serializer registered for Warning
^

SyntaxError: Unexpected token '<', "<w> [webpa"... is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18)

Node.js v20.11.1
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

 NX   Ran target build for project site (24s)

   ×  1/1 failed
   √  0/1 succeeded [0 read from cache]



### Package Manager Version

_No response_

### Operating System

- [X] macOS
- [ ] Linux
- [X] Windows
- [ ] Other (Please specify)

### Additional Information

_No response_

kartikye avatar Mar 11 '24 21:03 kartikye

Seeing this as well. 18.0.7 was fine so suspect it's very recently introduced.

PodStuart avatar Mar 12 '24 06:03 PodStuart

Also experienced the issue, reverted back to 18.0.7 and that seems to work.

johndalvik avatar Mar 13 '24 09:03 johndalvik

It looks like this is related https://github.com/nrwl/nx/pull/22190/files#diff-fcd96d0c9996c7987e1c72da9391cb2d845b36ab056886d02511dea284371c5dR140

KentoMoriwaki avatar Mar 13 '24 09:03 KentoMoriwaki

I have the same issue, works with 18.0.7 but not with 18.0.8. (Linux)

dlq84 avatar Mar 13 '24 10:03 dlq84

I see this with all patch versions 18.0.0..18.0.8 (downgraded one at a time from 18.0.8):

 ✓ Creating an optimized production build    
 ✓ Compiled successfully
   Skipping linting
   Checking validity of types  ..undefined:1
Failed to compile.
^

SyntaxError: Unexpected token 'Faile"... is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18)

v-karbovnichy avatar Mar 13 '24 11:03 v-karbovnichy

FYI: In my case, I could resolve the given error by transitioning my project to Inferred Tasks, a feature introduced in Nx 18. Details can be found at this link: https://github.com/nrwl/nx/issues/22295

The first step requires removing the "targets.build" field from your project.json file in your nextjs application.

@@ -1,24 +1,9 @@
 {
   "name": "myapp",
   "$schema": "../../node_modules/nx/schemas/project-schema.json",
   "sourceRoot": "apps/myapp",
   "projectType": "application",
   "targets": {
-    "build": {
-      "executor": "@nx/next:build",
-      "outputs": ["{options.outputPath}"],
-      "options": {
-        "outputPath": "dist/apps/myapp"
-      },
-      "configurations": {
-        "production": {
-        },
-        "development": {
-          "outputPath": "apps/myapp"
-        }
-      },
-      "defaultConfiguration": "production"
-    }
   },
   "tags": []
 }

Following this, a new configuration must be added to nx.json.

@@ -1,27 +1,36 @@
 {
   "$schema": "./node_modules/nx/schemas/nx-schema.json",
   "namedInputs": {
     "default": ["{projectRoot}/**/*", "sharedGlobals"],
     "production": [
       "default",
       "!{projectRoot}/.eslintrc.json",
       "!{projectRoot}/eslint.config.js",
       "!{projectRoot}/**/?(*.)+(spec|test).[jt]s?(x)?(.snap)",
       "!{projectRoot}/tsconfig.spec.json",
       "!{projectRoot}/jest.config.[jt]s",
       "!{projectRoot}/src/test-setup.[jt]s",
       "!{projectRoot}/test-setup.[jt]s"
     ],
     "sharedGlobals": []
   },
   "plugins": [
+    {
+      "plugin": "@nx/next/plugin",
+      "options": {
+        "buildTargetName": "build",
+        "devTargetName": "dev",
+        "startTargetName": "start",
+        "serveStaticTargetName": "serve-static"
+      }
+    }
   ],
   "generators": {
     "@nx/next": {
       "application": {
         "style": "css",
         "linter": "eslint"
       }
     }
   }
 }

This should mitigate the error in question.

KentoMoriwaki avatar Mar 13 '24 11:03 KentoMoriwaki

FYI: In my case, I could resolve the given error by transitioning my project to Inferred Tasks, a feature introduced in Nx 18. Details can be found at this link: #22295

Yes, but how to customize the build options with inferred tasks? for example the outputPath. the documentation of this feature is poor.

ShadiBlitz avatar Mar 14 '24 07:03 ShadiBlitz

I see this with all patch versions 18.0.0..18.0.8 (downgraded one at a time from 18.0.8):

 ✓ Creating an optimized production build    
 ✓ Compiled successfully
   Skipping linting
   Checking validity of types  ..undefined:1
Failed to compile.
^

SyntaxError: Unexpected token 'Faile"... is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18)

@v-karbovnichy,

Initially I thought the same, but then I realized that downgrading from 18.0.7 via nx migrate 18.0.7 was only updating the the nx package not @nx/next package where this issue exists. When I explicitly set all @nx/* packages in my repo to 18.0.7 I no longer encounter

SyntaxError: Unexpected token '(', "(node:7487"... is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18)

So an issue in 18.0.8, but not 18.0.7

nsmith7989 avatar Mar 14 '24 20:03 nsmith7989

Looks like the issue got fixed in 18.1.1.

ShadiBlitz avatar Mar 17 '24 09:03 ShadiBlitz

seems like this is still happening in 18.3.4:

 npx nx build ****

> nx run ****

   ▲ Next.js 14.1.3

   Creating an optimized production build ...
undefined:1
(node:64148) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
^

SyntaxError: Unexpected token '(', "(node:6414"... is not valid JSON
    at parse (<anonymous>)
    at parseChannelMessages (node:internal/child_process/serialization:152:15)
    at parseChannelMessages.next (<anonymous>)
    at channel.onread (node:internal/child_process:624:18)

Node.js v21.5.0

update

Though, I can confirm, removing the "build" from "targets" in project.json does solve the problem, but that's not entirely what I want.

zilahir avatar Apr 29 '24 22:04 zilahir

This should be addressed with 18.1.1 @zilahir can you create a new issue with a small reproduction?

ndcunningham avatar May 14 '24 19:05 ndcunningham

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.

github-actions[bot] avatar Jun 14 '24 00:06 github-actions[bot]