nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.51k stars 2.34k forks source link

Build nextJS app issue - Cannot read properties of null (reading 'message') #23162

Closed anteqkois closed 2 months ago

anteqkois commented 5 months ago

Current Behavior

When i build locally app, usign nx run web:build all works fine. But when I build docker image anr run RUN npx nx run web:build a got a misleading error:

332.1 ƒ Middleware                             40.3 kB
332.1
332.1 ○  (Static)   prerendered as static content
332.1 λ  (Dynamic)  server-rendered on demand using Node.js
332.1
332.8
332.8
332.8
332.8  NX   Successfully ran target build for project web and 2 tasks it depends on
332.8
332.8
332.8
332.8  NX   Cannot read properties of null (reading 'message')
332.8
332.8 Pass --verbose to see the stacktrace.
332.8
------
failed to solve: process "/bin/sh -c npx nx run web:build ${BUILD_FLAG}" did not complete successfully: exit code: 1

So I see that NextJS app was built, but the error broke my image build

Expected Behavior

No error durning docker build

GitHub Repo

No response

Steps to Reproduce

  1. create docker image and build it

Nx Report

Node   : 20.11.1
OS     : darwin-arm64
pnpm   : 8.15.2

nx (global)        : 18.3.4
nx                 : 18.3.4
@nx/js             : 18.3.4
@nx/jest           : 18.3.4
@nx/eslint         : 18.3.4
@nx/workspace      : 18.3.4
@nx/cypress        : 18.3.4
@nx/esbuild        : 18.3.4
@nx/eslint-plugin  : 18.3.4
@nx/nest           : 18.3.4
@nx/next           : 18.3.4
@nx/node           : 18.3.4
@nx/react          : 18.3.4
@nrwl/tao          : 18.3.4
@nx/web            : 18.3.4
@nx/webpack        : 18.3.4
typescript         : 5.4.5
---------------------------------------
Registered Plugins:
@nx/next/plugin
@nx/cypress/plugin
@nx/eslint/plugin
@nx/jest/plugin
@nx/webpack/plugin

Failure Logs

No response

Package Manager Version

pnpm 8.15.2

Operating System

Additional Information

Here is my docker file:

FROM linkerry-base:base AS builder

ARG NODE_ENV
ARG BUILD_FLAG

WORKDIR /app/builder

# comment this step now, project files alreaady exists
# COPY . .

RUN npx nx run web:build ${BUILD_FLAG}

FROM linkerry-base:base

WORKDIR /app
COPY --from=builder /app/builder ./
ENV NODE_ENV=$NODE_ENV

CMD ["nx", "run", "web:start"]
anteqkois commented 5 months ago

This issue also occurs when u build docker image for my nestJS app

Malagari commented 5 months ago

I had this issue when migrating to a newer version of NX (unsure if related) and found that force disabling the NX Daemon in my Dockerfile solved my issue: ENV NX_DAEMON=false

gcko commented 5 months ago

For all you folks who are searching the web for Cannot read properties of null (reading 'message') and pulling your hair out because your CI builds all of the sudden began failing:

The latest Gitlab update to Version 17 breaks NX's is-ci. I don't know why as the CI and GITLAB_CI variables are still provided FWIW, but it breaks 🤷

Adding ENV NX_DAEMON=false to your Dockerfile will fix it, thanks @Malagari !

EDIT: Looking at the latest changes to the is-ci package, it may be due to this change: packages/nx/src/utils/is-ci.ts

duwejeferson commented 4 months ago

Is this case related to logger_1.serverLogger.watcherLog('Unexpected workspace watcher error', error.message);? I'm having the same error when trying to start a nestJS api, when using the --verbose flag i got this

TypeError: Cannot read properties of null (reading 'message')
    at handleWorkspaceChanges (/home/duwejeferson/repositorio/ibs-erp-3/node_modules/.pnpm/nx@19.0.4_@swc-node+register@1.9.1_@swc+core@1.5.7/node_modules/nx/src/daemon/server/server.js:203:90)
    at /home/duwejeferson/repositorio/ibs-erp-3/node_modules/.pnpm/nx@19.0.4_@swc-node+register@1.9.1_@swc+core@1.5.7/node_modules/nx/src/daemon/server/watcher.js:39:9

If I execute a nx reset, the api start, but it doesn't restart with code changes, and if i kill it and try to execute again, the same error occurs.

Didn't debug any further. Maybe a should open a new Issue?

PS: I'm using Windows 11 with Ubuntu 22.04 on WSL

duwejeferson commented 4 months ago

Is this case related to logger_1.serverLogger.watcherLog('Unexpected workspace watcher error', error.message);? I'm having the same error when trying to start a nestJS api, when using the --verbose flag i got this

TypeError: Cannot read properties of null (reading 'message')
    at handleWorkspaceChanges (/home/duwejeferson/repositorio/ibs-erp-3/node_modules/.pnpm/nx@19.0.4_@swc-node+register@1.9.1_@swc+core@1.5.7/node_modules/nx/src/daemon/server/server.js:203:90)
    at /home/duwejeferson/repositorio/ibs-erp-3/node_modules/.pnpm/nx@19.0.4_@swc-node+register@1.9.1_@swc+core@1.5.7/node_modules/nx/src/daemon/server/watcher.js:39:9

If I execute a nx reset, the api start, but it doesn't restart with code changes, and if i kill it and try to execute again, the same error occurs.

Didn't debug any further. Maybe a should open a new Issue?

PS: I'm using Windows 11 with Ubuntu 22.04 on WSL

Adding a ? on error?.message "resolved" my issue, the API restarts properly with code changes now. 🤷‍♂️

boginw commented 4 months ago

I'm having the same issues as @duwejeferson on Ubuntu 22.04. I've tried using NPM, Yarn, and Bun, all with the same error as mentioned by @duwejeferson. I'm not using NextJS, but nx-dotnet. Seems to be an issue within Nx itself. Here's the block that brings the issue (nx/src/daemon/server/server.js:201):

if (err || !changeEvents || !changeEvents.length) {
    let error = typeof err === 'string' ? new Error(err) : err;
    logger_1.serverLogger.watcherLog('Unexpected workspace watcher error', error.message);
    console.error(error);
    workspaceWatcherError = error;
    return;
}

The issue seems to be that we can enter the if-statement's body if err is falsy if one of the other conditions is true, and when it is falsy, error.message will always fail.

boginw commented 4 months ago

Just found that this should be fixed in 19.1.0: https://github.com/nrwl/nx/pull/23500

ndcunningham commented 4 months ago

is this still an existing issue?

boginw commented 4 months ago

At least not for me. I resolved the issue by upgrading to 19.1.0

DorianNaaji commented 3 months ago

Hi, I'm experiencing this with nx 18.3.5 I'm running nx on my WSL like @duwejeferson

Linux 6.6.36.3-microsoft-standard-WSL2 x86_64 GNU/Linux Distributor ID: Ubuntu Description: Ubuntu 20.04.6 LTS Release: 20.04 Codename: focal

workaround provided by @gcko is working fine for me for nx 18.3.5

Will give a try to 19.x

ndcunningham commented 2 months ago

Closing as the issue has been fixed with an update.

github-actions[bot] commented 1 month ago

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.