Building a NodeJS app on Heroku deploy - node.js

What is the recommended approach to using build tools like Gulp and Browserify to build your app on deploy with Heroku? Heroku doesn't give much info other than it's possible.
The main reason I ask is because if you want to build your app on deploy you have to place your build tools in dependencies rather than devDependencies. That seems wrong to me and popular build tools like Gulp and Grunt instruct users to include them as dev dependencies so it seems like a common practice. I'm curious as to whether this is a subtle hint from Heroku to not build on deploy.
If Heroku is worried about dev dependencies polluting or compromising a production deployment npm prune --production can serve to prune non-production dependencies after postinstall scripts execute.

Related

Is it relevant to use dev dependencies in a Node / Express backend?

I understand using package.json dev dependencies when you build your code. But if your application is an Express backend, does it make any difference using dev or regular dependencies in package.json?
An example of a backend dev dependency I would use is nodemon.
The only thing I can think of is that hosting providers might somehow install only non-dev dependencies when running, but I can't find details if that actually happens.

How to deploy Node.js / Express app with typescript on heroku?

I want to deploy node.js / express server app on heroku.
I tried with this blog.
But it didn't work. The parts of my code are below.
server.ts
package.json
Ahh. You have messed up in the package.json file. What you have done is you have included the dependencies in the devDependencies.
Remember one thing always, when you are going to push an app to Heroku it prunes the devDependencies.
Actually, this is the main cause your server is not starting up because there are not required modules.
Run this command to move your devDependencies to dependencies:
npm install <module-name> --save-dev
This thing will solve your problem. If you face any other problem related to the configuration then follow this article - https://dev.to/hte305/simple-deploy-typescript-application-to-heroku-5b6g

ng build without node module download

Hi Is there a way to build/run ng cli command (ng build --prod) without downloading the npm packages every time?
The production build is very slow because of this reason, I wanted to check if this download can be avoided if we not adding any new node modules and using the existing one from the node module folder.
ng build --prod is not downloading any npm packages, but instead it's bundling those packages (as vendor-chunk) along with your app code.
The reason is slow in --prod is that because prod uses aot and build optimizer by default. All that time taken is for good cause, to bundle your code, tree shake it, uglify it...etc.
The only thing you can do in this case, is to make sure your code is not using any unnecessary 3rd party libraries, and most importantly you're using the latest ng-cli and Angular versions. keep up with the latest Angular updates, as the Angular Team is working very hard to make it awesome in every version they release.

How to deploy one app from a large monorepo with dependencies to packages in the same repo to google app engine?

I have a large node.js monorepo with several applications and packages and inter dependencies. It is all managed with yarn workspaces and a little bit of lerna. Everything works great for me, however I am having trouble trying to deploy one of the applications in this monorepo to google app engine.
The main issue is that the app engine wants to install packages that are located only locally and are not on npm, and it throws an error.
I've scoured the google cloud documentations but did not manage to find anything that I could use to specify custom node packages or anything similar.
Is there a way to make such a deployment without publishing the local packages to npm?
The basic structure of the app I want to deploy looks like this:
-root
-packages
-packageA
-package.json
-apps
-deployable-app
-package.json <-contains dependency: "packageA": "0.0.1"
-app.yaml
Create a minimal docker image by coping to the image only the deployable-app and the packages from the same monorepo that the deployable-app depends on (in your case it's packageA).
When installing, yarn will do all the work to link between them.
Notes:
Deterministic installation - It is recommended to do so in monorepos to force deterministic install because nodejs packag emanagers (pnpm, yarn, npm) does not read the lock files of the dependencies of your app so when you run install, and packageA is located in public/private npm-registry, the package manager will install packageA dependencies as he wants. But you have a yarn.lock file in your monorepo that described what exectly should be installed and in which version.
Small docker image, better caching - Copy only the local packages from your monorepo that your deployable-app needs and create a script that removed all devDependencies from all the package.jsons in the monorepo (inside the dockerfile). they are not needed in production. make your image small.
I have the same problem with Firebase Cloud Functions, so I decided to publish my packages into a private registry and configure the Cloud Function environment with a .npmrc to use my private registry. I suppose that you can do the same with App Engine.
For private registry, I already tried two: GitHub Package Registry(now in Beta) and Verdaccio(which is a self-hosted option)

Avoid npm refresh after every deployment on Heroku

I have a Node.js website hosted on Heroku, that I deploy with git.
I use several node modules, referenced in package.json; is there a way to prevent Heroku to 'refresh' them each time I deploy a new version of the code, as long as package.json did not change?
Note: this would be especially useful for 'native' modules, whose compilation takes a bit of time; for .js-only modules, I was successful removing them from package.json, and adding their node_modules/ folder in the git repo.
I'm the maintainer of the official Heroku Node.js Buildpack.
We have a new version of the buildpack in beta that features caching support, designed specifically for the use case you described above. You can read more about it at https://github.com/heroku/heroku-buildpack-nodejs/tree/diet#about-this-refactor
Eventually this will become the default Node.js buildpack on Heroku, but if you want to use it now you'll need to set the BUILPACK_URL config var explicitly:
heroku config:set BUILDPACK_URL=https://github.com/heroku/heroku-buildpack-nodejs#diet -a my-node-app
git commit -am "fakeout" --allow-empty
git push heroku
Seems like there has recently been progress by David Dollar at the heroku-buildpack-nodejs.
In short:
heroku config:set BUILDPACK_URL=https://github.com/ddollar/heroku-buildpack-nodejs
See answer here:
https://stackoverflow.com/a/18535675/1318839
You can add both .js and native npm packages and still avoid the "refresh", (at least the re-downloading part of the "refresh".)
Include the native packages to your node_modules/ directory. When you deploy to Heroku, npm install will skip the downloading of the package. npm rebuild is also run. It will take some time to re-compile the native packages, but it should be very bearable unless you have tons of native packages.
Sidenote: Heroku doc on what Heroku does when you push a nodejs app.
Sidenote: The npm rebuild is needed because there "are mysterious failures that can happen between node and native code modules after a node upgrade".
Clone the Heroku node.js buildpack, and modify it to remove the rebuild command.
The command is currently run here:
https://github.com/heroku/heroku-buildpack-nodejs/blob/master/bin/compile#L180 , but that's bound to change.
You can run create an app that uses your own buildpack by modifying the command below to match your own repo:
heroku create --buildpack http://github.com/heroku/heroku-buildpack-nodejs.git
Or change the buildpack of an existing app with:
heroku config:add BUILDPACK_URL=git://github.com/heroku/heroku-buildpack-nodejs.git

Resources