javascripttypescriptnestjsconfignomachine-nx

How do I force NX to use the project directory when running from the project root?


I have a monorepo with client and server projects.

My root project.json

{
  "name": "generic-social-network",
  "targets": {
    "client": {
      "command": "nx dev client"
    },
    "server": {
      "command": "nx dev server --exclude client"
    },
    "undefined": {
      "command": "nx server"
    }
  },
  "title": "generic-social-network"
}

./src/server/project.json

{
  "name": "server",
  "$schema": "../../node_modules/nx/schemas/project-schema.json",
  "title": "server",
  "targets": {
    "undefined": {
      "command": "nx dev server"
    },
    "dev": {
      "command": "nest start --watch"
    },
    "build": {
      "command": "nest build"
    },
...

In the root folder when I run npx nx the . directory resolves to the root folder not ./src/server as I would expect.

If I run nest start --watch in ./src/server everything compiles with no problems.

So, my question is, how do I tell nest to look in ./src/server and not in the root level folder?

I can't seem to find anything related on SO as of now, but if there's a related or duplicate question, please point me in the right direction.


Solution

  • You're using the shorthand command syntax. If you use the long form syntax, you can set the cwd property to tell NX to run the command in the project root instead of the monorepo root.

    "dev": {
        "executor": "nx:run-commands",
        "options": {
            "cwd": "src/server",
            "command": "nest start --watch",
        }
    }
    

    See docs: https://nx.dev/nx-api/nx/executors/run-commands