Who said you can’t code on a Chromebook? Local development with Cloud Functions.

Alessia Sacchi
Google Cloud - Community
6 min readDec 1, 2021

When I joined Google in January I was given the option to choose my laptop among several options across the main four platforms: Chrome, gLinux, macOS and gWindows. I liked the idea of starting my laptop in 5 seconds (my Samsung phone takes longer to turn on) but I can’t deny I was sceptical about coding. Well, the truth is it’s easy to dismiss Chromebooks and while they don’t use an OS on the same scale as Windows 10 or macOS, the platform has been maturing over time into something lightweight but still well featured.

This blog post describes the process of setting up Linux on a Chromebook and writing a simple HTTP Cloud Function using the Node.js runtime, Visual Studio Code and Google Cloud Functions Framework for Node.js. Let’s get started!

Photo by Oskar Yildiz on Unsplash

Set up Linux, Node and Visual Studio on your Chromebook

First things first, if you want to develop software on your Chromebook you will need to set up Linux. You can install Linux command line tools, code editors, and IDEs (integrated development environments) on your Chromebook. These can be used to write code, create apps, and more. If your device has Linux it will be off by default. You can turn it on any time from Settings. On your Chromebook, at the bottom right, select the time. Then select Settings > Advanced > Developers. Next to “Linux development environment,” select Turn On and follow the on-screen instructions. The setup of a Debian 10 (Buster) environment can take 10 minutes or more. You can run Linux commands, install more tools using the APT package manager, and customize your shell. A good first thing to do is update the APT⁠ repository index and install. APT is a command-line package manager for Debian, and keeping it up to date will ensure you’re always installing the latest versions of tools in its repository:

Running `apt-get update`

Hint! A simple way to access files in the Linux container is to make a copy. To do so, open the Files app, drag whatever directory or file you want to access, and drop it in “Linux files”.

Now that Linux is set up, to install Node.js we need to make sure that we’re running the installer commands as root (at time of writing the current version of Node is 17.1.0).

sudo /bin/bash
curl -fsSL https://deb.nodesource.com/setup_17.x | bash -
sudo apt-get install -y nodejs
Installing the NodeSource Node.js 17.x repo in the terminal

If you run into errors due to any existing conflicting versions of Node clean up your environment by running:

sudo apt remove — purge nodejs node npm
sudo apt clean
sudo apt autoclean
sudo apt install -f
sudo apt autoremove

Now that a Crostini Linux container is set up and Node is installed, we are going to move on to Visual Studio Code. What is it? It is a lightweight but powerful source code editor which runs on desktops and is available for Windows, macOS and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (such as C++, C#, Java, Python, PHP, Go) and runtimes (such as .NET and Unity).

While you can always install apps and tools in the terminal via sudo apt install, Linux on Chrome OS supports double-click to install for .deb files (Debian software package) in the Files app, allowing you to download and install apps like you’re used to. Visual Studio Code, for instance, offers a .debpackage, that you can try this with. Once downloaded open the Files app, right-click and select “Install with Linux (Beta)”. You’ll see a prompt with information about the app you’re looking to install, along with the option to install the app. From here on out you’ll be able to launch Visual Studio Code from the app drawer like any other application on your Chromebook once the Linux installer has finished. You can also launch it from the terminal simply by typing code.

Visual Studio Code on Chrome OS

Create and test a HTTP Cloud Function locally

Google Cloud Functions is an event-driven serverless compute platform. Cloud Functions allows you to write your code without worrying about provisioning resources or scaling to handle changing requirements. Because testing code on Cloud Functions itself involves waiting for deployed code and log entries to become available, running and testing your function on your development machine can make the testing process (and, in turn, the development process) significantly faster.

Before we create and test the Cloud Function in Visual Studio there’s one last step we need to do as part of the local dev environment setup: install the Functions Framework for Node.js. The Google Cloud Functions team has created an open source FaaS (Function as a Service) framework for writing portable Node.js functions that run in many different environments, including Google Cloud Functions, your local development machine, Cloud Run and Cloud Run on GKE and Knative-based environments.

So, let’s create a new Node.js app by running npm initin the terminal. While accepting defaults, make sure to use index.js as the entry point for your app.

mkdir ~/helloworld
cd ~/helloworld
npm init
Create a simple HTTP Cloud Function using npm init

Then install the Functions Framework for Node.js (npm install @google-cloud/functions-framework). Open your package.json. Verify that you see the functions framework listed as a dependency as shown in the example below.

Install the Functions Framework

We are now ready to edit the Cloud Function in Visual Studio Code. Click the Explorer icon in the IDE and open the folder you have just created. Now create an index.js file in the helloworld directory and add some hello world code :-). To test the the function using the integrated terminal within Visual Studio open a terminal window and run the following command:

npx functions-framework --target=helloWorld

This command starts a local server that is ready to call the helloWorld function when the server receives an HTTP request. To test the function create a second terminal window and run the following command to send a name payload to the local server serving the helloWorld function.

curl -X POST http://localhost:8080 -H "Content-Type:application/json"  -d '{"name":"Alessia"}'

Now that you’ve created and tested the Cloud Function on your local machine, you are ready to deploy it to Google Cloud. Before doing that though, we need to make sure all the node_modules are going to be excluded from the deployment when we deploy to Google Cloud. For this specific purpose we need to add a file called .gcloudignore in the top-level directory. This .gcloudignore prevents the upload of the node_modules/ directory and any files ending in ~:

/node_modules/
*~

Deploy a Cloud Function from a local machine to Google Cloud

Now that we have packaged the application for Cloud Functions, the next step is to deploy it to Google Cloud. To do this we need to install Cloud SDK locally and perform several common Cloud SDK setup tasks using the gcloud init command. These include authorizing the Cloud SDK tools to access Google Cloud using your user account credentials and setting up the default configuration (make sure to set the project ID not the project number).

Once your Google Cloud SDK is configured and ready to use in any terminal window run the following command:

gcloud functions deploy helloWorld --trigger-http --runtime nodejs12 --allow-unauthenticated

When deployment is completed, you will see the following in the output:

We’re done! Thanks for reading this blog post where we’ve explored how to streamline local development with Cloud Functions for Node.js using Visual Studio Code on a Chromebook.

--

--