Deploying a NestJS app

Hey there.

I built a REST API using NestJS and I would like to deploy it using fly.io. I followed the steps in the quickstart guide and a Dockerfile and fly.toml file were created automatically for me. It seems that fly has treated my app as a regular express app and created the files accordingly. What can I change to make this deploy properly?

Presently, when I run fly deploy, I get an error message saying

#11 10.17 sh: 1: nest: not found
------
Error failed to fetch an image or build from source: error building: executor failed running [/bin/sh -c npm install && npm run build]: exit code: 127

I am also linking my repo below in case you need it.

Thanks

1 Like

Hi,

Hmm. The error says that nest is not found, so that sounds like your problem. And it would try and use it, since e.g the first thing the Dockerfile is going to do is:

… which relies on nest being available. Locally that is probably in your PATH and so you can simply type nest and it’s there. But it won’t be on the remote builder. I’d assume the NestJS CLI dev dependency is installed as part of that first half, the build step, but that would be a local copy it, not a global install.

Total guess from now on as I haven’t used NestJS before but I’d think you could add a line in your Dockerfile prior to calling npm run build for e.g RUN npm i -g @nestjs/cli

Installing it globally should mean it is then available.

Else you could add it to the PATH so it would then be found

Or use npx to run it (so change nest X to npx @nestjs/cli X) so again, it would then be found as it would then use the local copy you’ve installed as a dev dependency.

Any of those should mean you proceed beyond that particular error.

1 Like

I ended up finding a solution. The Dockerfile linked here did the trick for me.

i just ran into exactly the same issue and i think i have a more correct solution

the reason nest is not found is because @nestjs/cli is a devDependency so it doesn’t get installed in production. my fly.io dockerfile sets NODE_ENV to production, but also included a helpful comment which addresses this exact issue

# ...
# NPM will not install any package listed in "devDependencies" when NODE_ENV is set to "production",
# to install all modules: "npm install --production=false".
# Ref: https://docs.npmjs.com/cli/v9/commands/npm-install#description

ENV NODE_ENV production

COPY . .

RUN npm install && npm run build
# ...

i fixed this by following the instructions in the comment :

- RUN npm install && npm run build
+ RUN npm install --production=false && npm run build

Hey Patrik, I think you’re answer is technically correct given what my question was. But upon further observation, I think my question was flawed. The real solution would be to use a dockerfile that is appropriate for a nest project (which I wasn’t doing). I linked a better dockerfile to use when making nest projects.