DEV Community

Cover image for Nodejs cloud function deployment with private npm packages
Kanthaliya
Kanthaliya

Posted on • Updated on

Nodejs cloud function deployment with private npm packages

Cloud Functions is an event-driven serverless compute platform from Google. You can run your code locally or in the cloud without having to provision servers. There are many ways to run nodejs application on cloud function. we are going to look for zip upload and deploy code from terminal.

On Google cloud platform select cloud function and create function.
Alt Text

Fill the required information

  1. Name - function name for your program.
  2. Memory allocated - as required by function processing.
  3. Trigger - select Http
  4. Source code - zip upload.
  5. Runtime - nodejs 10
  6. Function to execute - It is a name of a function exported by the module specified in the directory with source code.
  7. Check Advance Options if you want more control. Alt Text

and finally upload a zipped nodejs application.

To deploy nodejs application with private npm package, include .npmrc file at root level -

//npm.private.com/:_authToken="<token>"
@ng-test:registry=https://npm.private.com/ 
Enter fullscreen mode Exit fullscreen mode

you can read private npm package auth token from ~./npmrc or login with npm login --registry=https://registry.company-name.npme.io and npm token list.

In package.json add private package name in dependencies and Google functions-framework in dev dependencies.

{
    "name": "notification-service",
    "version": "1.0.0",
    "description": "Notification Service",
    "main": "index.js",
    "author": "Pritesh Kanthaliya",
    "license": "UNLICENSED",
    "scripts": {
        "start": "npx @google-cloud/functions-framework --target=notificationService",
        "deploy": "npx gcloud functions deploy notificationService --runtime nodejs10 --trigger-http",
    },
    "dependencies": {
        "@ng-test/hello-world": "1.0.0",
    },
    "devDependencies": {
        "@google-cloud/functions-framework": "~1.5.1",
    }
}
Enter fullscreen mode Exit fullscreen mode

With npm start you can run nodejs application at local machine and test. With npm deploy you can deploy your code on Google cloud function. If a cloud function is already present with the same name, it overrides the current one else it would create a new one.

Top comments (0)