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)
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.
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 tsc
on 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.
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
To deploy Nest JS app to Elasticbeanstalk (EBS) you need just 3 steps:
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.
Procfile
in the root of sources, containingweb: npm run deploy
eb deploy
Enjoy!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With