This is the first time I'm using Vercel, so any help would be appreciated. I have a monorepo project with the following structure:
- /project
- /app
- /server
- /src
- package.json
- tsconfig.json
- /shared
The server is a Nodejs application written with Typescript. To run it locally, I just run npx ts-node -r tsconfig-paths/register --transpile-only src/index.ts
, which is my start
script.
I've tried to deploy this service to Vercel so that it kicks off the server by running yarn start
in this same manner, but that doesn't seem to work:
From what I've read, it looks like as part of the deployment process, Vercel requires me to (1) explicitly transpile the TS code into JS, and (2) have a index.js
file at the root of the transpiled directory. I've also read in some places (e.g. here) that this directory must be called api
for the deployment to succeed.
I've written a build
script that transpiles the code to JS and I've verified that I'm able to run the server from the JS file using node api/server/src/index.js
. However, I'm still getting the NOT_FOUND
error in the image, and I image that's because the index.js
file is not at the root of the api
folder. However, I'm afraid this is not possible given that my server
is part of a monorepo, which means that the transpiled api
directory has the following structure:
- /api
- /server
- /src
- index.js
- /shared
Does anyone know how I should proceed to deploy my server in this case? Taking a step back, do I even need to worry about the build step at all or is there a way to have my server spun up by just running the ts-node
command I mentioned above? Note that during all my attempts, there were no errors during the deployment process.
Example
No response
Steps to Reproduce
Print of the end of the build log:
Deployed files have the structure I outlined above:
Deploying Express apps in a serverless environment does require a little different setup from a typical server. There is a guide for deploying Express.js with Vercel that helps with most common issues: https://vercel.com/guides/using-express-with-vercel
The advice about using an /api
directory comes from the Node.js runtime, so I think this documentation may be helpful as well: https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js
The Node.js Runtime, by default, builds and serves Serverless Functions within the /api
directory of a project, providing the files have a file extension of .js
, .mjs
, or .ts
.
Did you configure vercel.json in the root of the repository?
From the little I know about Vercel,
every Node.js application without a defined framework, such as a site that runs with a server and express, needs the vercel.json file.
algo como
{ "version": 2, "builds": [ { "src": "src/index.ts", "use": "@vercel/node" } ], "routes": [ { "src": "/(.*)", "dest": "src/index.ts" } ] }
Deploying Express apps in a serverless environment does require a little different setup from a typical server. There is a guide for deploying Express.js with Vercel that helps with most common issues: https://vercel.com/guides/using-express-with-vercel
The advice about using an /api
directory comes from the Node.js runtime, so I think this documentation may be helpful as well: https://vercel.com/docs/concepts/functions/serverless-functions/runtimes/node-js
The Node.js Runtime, by default, builds and serves Serverless Functions within the /api
directory of a project, providing the files have a file extension of .js
, .mjs
, or .ts
.
My issue ended up being typescript related. For some reason the "allowImportingTsExtensions" setting wasn't allowed in tsconfig and I had to use js imports because I couldn't find a way to allow it. If you're not using the CLI, I recommend it, it's easier to debug with.
https://vercel.com/docs/cli
https://vercel.com/docs/cli/project-linking
https://vercel.com/docs/cli/global-options
Besides to Amyegan's answer, I see they also published a article about this. Might be useful for future visitors.
Vercel Deploying-yarn-monorepos-to-vercel