10

I'm quite surprised about the lack of resources that show how to deploy a NestJS app. I struggling to do get this done (after solving this, I'll probably write an article to just provide a tutorial for the standard use-case).

I have a small, standard NestJS MVC app that I want to host on aws Elastic Beanstalk (using the CLI).

I don't see the log for the server starting up instead the logs show:

May 12 11:01:01 ip-172-31-31-53 web: Error: Cannot find module '/var/app/current/dist/main'
May 12 11:01:01 ip-172-31-31-53 web: at Function.Module._resolveFilename (internal/modules/cjs/loader.js:980:15)
May 12 11:01:01 ip-172-31-31-53 web: at Function.Module._load (internal/modules/cjs/loader.js:862:27)
May 12 11:01:01 ip-172-31-31-53 web: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
May 12 11:01:01 ip-172-31-31-53 web: at internal/main/run_main_module.js:18:47 {
May 12 11:01:01 ip-172-31-31-53 web: code: 'MODULE_NOT_FOUND',
May 12 11:01:01 ip-172-31-31-53 web: requireStack: []
May 12 11:01:01 ip-172-31-31-53 web: }
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! code ELIFECYCLE
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! errno 1
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! [email protected] start:prod: `node dist/main`
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! Exit status 1
May 12 11:01:01 ip-172-31-31-53 web: npm ERR!
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! Failed at the [email protected] start:prod script.
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
May 12 11:01:01 ip-172-31-31-53 web: npm ERR! A complete log of this run can be found in:
May 12 11:01:01 ip-172-31-31-53 web: npm ERR!     /home/webapp/.npm/_logs/2020-05-12T11_01_01_151Z-debug.log

I suspect the problem may arise because some TypeScript dev-dependencies are not installed on the production server, but I don't really know how to solve this

What I did so far:

Created a Procfile to overwrite the default Node Command (Procfile):

web: npm run start:prod

Changed the port for my application (main.ts)

  await app.listen(process.env.PORT || 3000);
  console.log('server start on PORT' + process.env.PORT)
  console.log(process.env.EMAIL_USER)
5
  • 1
    Did you run the build command in your deployment environment? Or did you deploy the dist directly and only install the dependencies? Typescript requires you to transpile (build) to JavaScript before the code can be ran by node. If you haven't ran the build and you didn't deploy the dist directly, you won't have a dist/main file to run. Commented May 12, 2020 at 16:42
  • Did you come to a good solution? Commented Jun 2, 2020 at 20:16
  • Hm, yes. The steps above worked for me (change the port, create procfile). I did experiment with putting some dev dependencies into the normal dependencies within my package.json but I don't think this was the cause. For me, a major problem was that I hadn't whitelisted my server IP for my MongoDB cluster. Commented Jun 3, 2020 at 8:23
  • @Xen_mar Did you manage to find any resources, and if so, could you share them? Commented Sep 23, 2020 at 22:18
  • @JayMcDoniel - understood, but is this typically done manually, then deployed? Or are there services (like Heroku?) that you can just push your source code to and then they transpile to js and run? Is Docker the way to go? Any resource, blog post, videos, paid or unpaid, I'm desperate to hear about :) Commented Sep 23, 2020 at 22:20

5 Answers 5

2

To deploy Nest JS app to Elasticbeanstalk (EBS) you need just 3 steps:

  1. Modify your package.json file and add there "deploy" script like this one:
{
  ...
  "scripts": {
   ...
   "deploy": "npm ci && npm run build && npm run start:prod"
  }
}

you need to install all (including dev) dependencies to be able build and run Nest app.

  1. Create file Procfile in the root of sources, containing
web: npm run deploy
  1. Commit to GIT, Push to repo, and do eb deploy

Enjoy!

Sign up to request clarification or add additional context in comments.

1 Comment

why do you also install dev dependencies ?
1
+50

Your main.ts looks fine! For the node command you can change its setting from Container Options in the beanstalk configuration screen.

NestJS's starter has a default working tsconfig.json, run tscon a terminal with TypeScript installed. Running tsc will build all of our typescript files into the dist folder. The application entrypoint will be on dist/src/main.js.

You also need to copy all non-typescript configuration files, including package.json, to the dist folder.

5 Comments

Thanks for the answer! I solved my problem by now. But just for reference. AWS does not let you set the Node Command over the GUI anymore. Instead, you have to include a Procfile to your project. Also, calling tsc is most likely not necessary. Nest also has a dedicated prebuild script that is probably better suited for transpiling.
That's great but I do agree there is little resources about deploying out there! Thanks for updating my AWS knowledge, haven't used it in awhile!
I'll definitely write an article on deploying a nest project to AWS real soon! :-D
Looking forward send me a link when you're done! :)
Hi, @Xen_mar I also want to know how you deployed nest js app to Beanstalk. Do you have any guide written? Much appreciated. Thank you very much.
1

The default command that is run for a node app by node platform is npm start - which for a nest app is nest start . The nest commands are part of nest cli - which are not available by default. A simple solution would be to copy below dev dependencies to the main dependencies block in package.json This will ensure that nest cli is installed - the app compiled to .js - and nest start works as expected.

@nestjs/cli, @nestjs/schematics,tsconfig-paths , typescript, @types/express , @types/node, ts-node

Comments

0

I got stuck with the deploying the nestjs app to AWS elastic beanstalk for a while I make the deploy guide step by step for deploying the simple app.

Maybe this help!

Comments

0

If you are using CI/CD make sure to add all the files that you need to the artifact/files sections in your buildspec.yml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.