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)
distdirectly and only install thedependencies? Typescript requires you to transpile (build) to JavaScript before the code can be ran bynode. If you haven't ran the build and you didn't deploy thedistdirectly, you won't have adist/mainfile to run.