paint-brush
Private NPM Packages with Google Cloud Functionsā€‚by@mikenikles
3,854 reads
3,854 reads

Private NPM Packages with Google Cloud Functions

by Mike NiklesDecember 20th, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

<em>tl;dr: It is now officially </em><a href="https://cloud.google.com/functions/docs/writing/dependencies#using_private_modules" target="_blank"><em>supported</em></a><em> šŸŽ‰!</em>

Company Mentioned

Mention Thumbnail
featured image - Private NPM Packages with Google Cloud Functions
Mike Nikles HackerNoon profile picture

tl;dr: It is now officially supported šŸŽ‰!

As part of an AWS to GCP migration, we wanted to leverage Google Cloud Functions (https://cloud.google.com/functions) to help us scale our infrastructure in a more manageable manner.

Cloud Functions are written in JavaScript and execute in a standard Node.js runtime environment.

How great is that?ā€Šā€”ā€Šso we thought. Instead of migrating our AWS monolith, we decided to extract isolated pieces and deploy them as independent services, one piece at a time. Our existing application is written in Node.js and migrating to GCF (Google Cloud Functions) sounded straight forward. If it wasnā€™t for our private NPM packagesā€¦

A note for clarity, GCF has always worked beautifully if you only rely on public npm packages.

Private NPM Packagesā€Šā€”ā€ŠTheĀ Problem

In order for our teams to share common code, we use private npm packages. GCF did not like thatā€¦ Thereā€™s a StackOverflow discussion with a workaround. It works great if your code has top-level dependencies that are private npm packages. If, like in our case, you have transient dependencies that are also private npm packages, you either get really creative with your scripts or look for alternatives. In our case, we deployed to Google App Engine, which was a temporary workaround as it becomes very expensive very quicklyā€¦

Private NPM Packagesā€Šā€”ā€ŠTheĀ Solution

Yesterday, I ended up on the GCF docs by accident (yeah stuff like that happens) and found a new chapter ā€œUsing private modulesā€. Wait what? Whaaaat?

In order to use a private npm module, you have to provide credentials (auth token) for the npm registry in aĀ .npmrc file located in the function's directory. You can simply copy theĀ .npmrc file that was created in your home directory when you logged into npm using the npm login command.

Thatā€™s the entire chapter and it is such a game changer!

Let me know in the comments if this makes your decision simpler to migate to GCF. Have questions? Ask away, Iā€™m happy to help out wherever I can.