Ad

Production Deployment Failing When Using CLI Package.json Scripts

I have created a basic application through the CLI tool (v6.3.0) and have reached the point where I was ready to push to a production server and came across an issue.

I am deploying using Shipit (which is probably not relevant) and part of this process is to install the npm dependencies (with the flag --production).

Everything deploys without a hitch, until my deployment runs the final command npm run start:prod (to start the Nest application on the node process). Which executes the following steps:

  1. Deletes the contents of the dist directory rimraf dist && npm run build;
  2. Tries to rebuild the contents of the dist directory tsc -p tsconfig.build.json;
  3. Runs node dist/main.js;

The problem with all this, is that the TypeScript package that I believe provides the tsc command is a devDependency not a dependency that is installed during npm install with the --production flag.

Is this a bug, or am I completely missing the point of the npm run start:prod command. I appreciate that installing the TypeScript package globally may resolve the issue, although I wasn't sure if that was the intention, or assumption?

If you deploy NestJS to production, are you utilising some other strategy?

Ad

Answer

In server-side applications the distinction between dependencies and devDependencies is not as important, since you're not shipping your dependencies and hence a the size of your application is not as critical.

However, when you deploy to the cloud you might want to save space. TypeScript is not needed to run your application once it is compiled. And you don't need to recompile your application when you simply want to restart it. So, what you could do instead:

Remove the prestart:prod script.

Deployment routine:

  1. Install all dependencies with npm install
  2. Run npm run build
  3. Prune your dev dependencies with npm prune --production
  4. Run npm run start:prod

This is pretty much what happens when I deploy to heroku.

Alternatively, you can of course declare typescript under dependencies instead of devDependencies.

Ad
source: stackoverflow.com
Ad