Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deploying NestJs to ElasticBeanstalk

Tags:

node.js

nestjs

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)
like image 747
Xen_mar Avatar asked May 12 '20 11:05

Xen_mar


People also ask

How do I deploy NestJS app?

To do this: Create a new Git repository through your Git provider. Run git remote add origin <Your Git repository URL> to link the newly created repository with the repository the NestJS CLI initiated for you when creating the project. Commit your changes and push to the repository.


3 Answers

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.

like image 56
Waleed Avatar answered Oct 28 '22 09:10

Waleed


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

like image 1
xan_z Avatar answered Oct 28 '22 09:10

xan_z


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!

like image 1
Alexey Petushkov Avatar answered Oct 28 '22 09:10

Alexey Petushkov