tatethurston / nextjs-routes

Type safe routing for Next.js
MIT License
571 stars 23 forks source link

Adds `cwd` to help with monorepos #91

Closed alexgorbatchev closed 1 year ago

alexgorbatchev commented 1 year ago

Adds an option to pass cwd to avoid having to do this workaround https://github.com/tatethurston/nextjs-routes/issues/73#issuecomment-1299529571

tatethurston commented 1 year ago

Before adding a configuration option, I’d like to understand how Nextjs is resolving your pages directory in your project. I would expect the Nextjs process to encounter the same issue. If it doesn’t, I’d like to mirror their approach.

JavaScript monorepos generally define a package.json per package or application in the monorepo, and the working directory is resolved relative to that package.json when executing commands across the repo (lerna, yarn workspaces, etc).

Could you share a minimal example project with your Nx setup? https://github.com/tatethurston/nextjs-routes/issues/73#issuecomment-1300676454 and share how you’re invoking next commands?

alexgorbatchev commented 1 year ago

@tatethurston sure, try this https://stackblitz.com/edit/nextjs-bzas5l?file=README.md

image

I just installed nx and generated next app with it, and added nextjs-routes, nothing else.

alexgorbatchev commented 1 year ago

JavaScript monorepos generally define a package.json per package or application in the monorepo, and the working directory is resolved relative to that package.json when executing commands across the repo (lerna, yarn workspaces, etc).

Nx monorepo setup does support that, however default setup is a single package.json file in the root. https://nx.dev/getting-started/integrated-repo-tutorial

tatethurston commented 1 year ago

I looked into this and the next commands accept a directory as their first argument, otherwise falling back to the cwd. I think (but haven’t yet confirmed) this is how the nx next package is working.

I think we should be able to do the same from process.argv so clients don’t need to configure anything

alexgorbatchev commented 1 year ago

I believe that the issue here is that the NX builder doesn't allow for that property to be passed to to next

tatethurston commented 1 year ago

@alexgorbatchev Is additional configuration necessary in your nx example? When I remove withRoutes from nextjs-routes, I see the following nextjs error:

Error: > Couldn't find a `pages` directory. Please create one under the project root
    at Object.findPagesDir (/home/projects/nextjs-rv54c8/node_modules/next/dist/lib/find-pages-dir.js:42:19)
    at new DevServer (/home/projects/nextjs-rv54c8/node_modules/next/dist/server/dev/next-dev-server.js:107:66)
    at NextServer.createServer (/home/projects/nextjs-rv54c8/node_modules/next/dist/server/next.js:137:20)
    at eval (/home/projects/nextjs-rv54c8/node_modules/next/dist/server/next.js:149:42)

https://stackblitz.com/edit/nextjs-rv54c8?file=myorg%2Fpackages%2Fweb%2Fnext.config.js

alexgorbatchev commented 1 year ago

i cleaned up the demo, it was trying to npm start from root, try cd myorg && npm start

https://stackblitz.com/edit/nextjs-xawu1x?file=myorg%2Fpackage.json,myorg%2Fpackages%2Fweb%2Fnext.config.js

tatethurston commented 1 year ago

Did some digging into the nx nextjs wrapper because I wanted to understand how they are resolving the pages directory. nx creates a custom nx server: https://github.com/nrwl/nx/blob/94edbe9f6ce808ec9296954ea4333d7698f34e1e/packages/next/src/executors/server/server.impl.ts#L94 and passes in the project directory.