Skip to content

Cannot Serve Project from Project Root for a NestJS project #30087

@MisterPandaPooh

Description

Current Behavior

When running nx run user-service:serve from the project root, I encounter an error after updating from nx@18.2 to nx@latest. However, executing the same command from the workspace root works correctly. This issue wasn't present with nx@18.2.

The issue seems to appears only in nest project, I tested react project Its not happening.

Expected Behavior

To enable running nx nest targets not only from the workspace root but also from the project root.

GitHub Repo

https://github.com/MisterPandaPooh/nx-cwd-issue

Steps to Reproduce

  1. Create a new workspace with nx@18.2.4, using yarn@1.22.22, including one nest project in service/user-service and one react project with Module Federation in platform/ui-host and platform/ui-remote.
# Create a new workspace
npx create-nx-workspace@18.2.4  #(No Stack, Integrated Monorepo)
yarn

# Add the following plugins
nx add @nx/react
nx add @nx/express
nx add @nx/nest
nx add @nx/node
nx add @nx/plugin
nx add @nx/cypress

# Generate the host project (not really related to the issue)
nx generate @nx/react:host --name=ui-host --directory=platform/ui-host --e2eTestRunner=none --unitTestRunner=jest --minimal=true --projectNameAndRootFormat=as-provided --setParserOptionsProject=true --style=none --no-interactive
nx generate @nx/cypress:configuration --project=ui-host --setParserOptionsProject=true --no-interactive


# Generate the remote project  (not really related to the issue)
nx generate @nx/react:remote --name=ui-remote --directory=platform/ui-remote --host=ui-host --e2eTestRunner=none --projectNameAndRootFormat=as-provided --setParserOptionsProject=true --style=none --unitTestRunner=jest --no-interactive
nx generate @nx/cypress:configuration --project=ui-remote --setParserOptionsProject=true --no-interactive

# Generate the nest project
nx g @nx/nest:application user-service --directory=services/user-service --projectNameAndRootFormat=as-provided
  1. Run nx run user-service:serve in the root of the workspace or in the service/user-service directory. The nest project will start successfully.

  2. Migrate to nx@latest.

npx nx migrate latest
npx nx migrate --run-migrations
  1. Run nx run user-service:serve in the root will work fine. But running the same command in the service/user-service directory will throw the following error:
Debugger attached.
Waiting for the debugger to disconnect...
/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:331
    throw new Error(`Could not find ${fileToRun}. Make sure your build succeeded.`);
          ^

Error: Could not find dist/services/user-service/main.js. Make sure your build succeeded.
    at fileToRunCorrectPath (/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:331:11)
    at /Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:114:49
    at new Promise (<anonymous>)
    at Object.start (/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:108:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async processQueue (/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:79:13)

Node.js v20.12.2
Waiting for the debugger to disconnect...

Note: I only added react project to reflect my nx workspace setup. There is no direct link with react project.

Nx Report

Node           : 20.12.2
OS             : darwin-arm64
Native Target  : aarch64-macos
yarn           : 1.22.22

nx (global)            : 20.4.2
nx                     : 20.4.4
@nx/js                 : 20.4.4
@nx/jest               : 20.4.4
@nx/eslint             : 20.4.4
@nx/workspace          : 20.4.4
@nx/cypress            : 20.4.4
@nx/devkit             : 20.4.4
@nx/eslint-plugin      : 20.4.4
@nx/express            : 20.4.4
@nx/module-federation  : 20.4.4
@nx/nest               : 20.4.4
@nx/node               : 20.4.4
@nx/plugin             : 20.4.4
@nx/react              : 20.4.4
@nx/web                : 20.4.4
@nx/webpack            : 20.4.4
typescript             : 5.7.3
---------------------------------------
Registered Plugins:
@nx/cypress/plugin
@nx/webpack/plugin
@nx/eslint/plugin

Failure Logs

Debugger attached.
Waiting for the debugger to disconnect...
/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:331
    throw new Error(`Could not find ${fileToRun}. Make sure your build succeeded.`);
          ^

Error: Could not find dist/services/user-service/main.js. Make sure your build succeeded.
    at fileToRunCorrectPath (/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:331:11)
    at /Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:114:49
    at new Promise (<anonymous>)
    at Object.start (/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:108:36)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async processQueue (/Users/super.user/Documents/Workspaces/nx-issue-cwd/node_modules/@nx/js/src/executors/node/node.impl.js:79:13)

Node.js v20.12.2
Waiting for the debugger to disconnect...

Package Manager Version

yarn@1.22.22

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

⚠ Interesting observation: ⚠

It appears to be related to the workspace root. When printing the fileToRun value here, I'm getting an absolute path in nx@18, but a relative path in nx@20.

nx@18: fileToRun = /Users/super.user/Documents/Workspaces/nx-issue-cwd/dist/services/user-service/main.js

nx@20: fileToRun = dist/services/user-service/main.js

Additionally, when I make this small change in the getFileToRun function on line 407, the issue seems to be resolved.

-       workspaceRoot: '',
+       workspaceRoot: context.root,

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions