How do I install the Airtable package to a cloud function, so I can call the airtable package on a serverless tool like Google Cloud Functions, AWS Lambda, IBM Cloud Functions, or Azure Functions

Hi all,

I’m at my wits end. I’ve written code for Airtable locally, which largely works. Now I’d like to place the code on a cloud functions tool so I can run my code on the browser and have it communicate with other APIs.

Here’s the code I have on the serverless tool, just the basics first just trying to call the airtable library.

/**
  * main() will be run when you invoke this action
  *
  * @param Cloud Functions actions accept a single parameter, which must be a JSON object.
  *
  * @return The output of this action, which must be a JSON object.
  *
  */

var Airtable = new require('airtable');

var base = new Airtable({apiKey: 'XXXXXXXXXXXXX'}).base('appumWMhUzhgN8fFe');

function main(params) {
	return { message: 'Hello World' };
}

When I try to run this code, I get

Error: Cannot find module ‘airtable’\n


In this case I’m using IBM Cloud Functions, but the process as I also read Google Cloud Functions and others seems to be the same. I just am doing something wrong. Here’s the link to the docs

https://cloud.ibm.com/docs/openwhisk?topic=openwhisk-prep#prep_js_npm

Here are the steps I’ve taken.

  1. I create a new folder in my directory called “package-airtable”. I copy the “airtable” folder from j_mai > node_modules > airtable, into the “package-airtable” folder. So I have a new folder in “j_mai > package-airtable” with the airtable folder you see below.

  1. I then create a package.json file in the folder “package-airtable” with the name: “my-action” and “dependencies” “airtable”: “^0.10.1”
{
  "name": "my-action",
  "dependencies" : {
    "airtable": "^0.10.1"
   }
}
//contents of my package.json

  1. Then I create a zip folder, called “action.zip” with the package.json and the “airtable” library.

  1. Finally, I upload the zip file to the default package using this code on the command line.

$ ibmcloud fn action create package-airtable action.zip --kind nodejs:10

You can see here the “package-airtable” gets created.


To test

  1. I’m not sure if I’m supposed to, but I click “Invoke” on the “package-airtable” and I get an error message about not being able to find a module nodejsAction.

  1. If I go back to my “airtabledefaultest”, I run into the same error at the beginning, "Cannot find module ‘airtable’\n.


Any help would be greatly appreciated!

Hey @J_Mai,

I literally just wrote a tutorial on this topic for AWS Lambda:

The tutorial uses a tool we built for Airtable, Sync Inc, but it covers this process for AWS Lambda and I think it will apply here.

For your issue, I would make sure you are setting up the Node.js directory properly. Go through this flow:

  1. Set up your directory using npm init -y
  2. Install Airtable JS and save it using npm → npm install airtable --save
  3. Then zip up the all the files (including the npm modules directory) using zip -r (you’ll see the full details in the tutorial) and then upload it.

This works well for AWS Lambda. I haven’t tried it on IBM cloud but would love to hear if these steps solve your issue.

Eric

2 Likes

Yes, your article largely worked!

One thing I needed additionally was to define a main() function in index.js. And also add export.main = main; for some reason, to deal with an error message when export wasn’t listed.

//contents of my index.js file

var Airtable = new require('airtable')

var base = new Airtable({apiKey: 'XXXXXXXXXXXXX'}).base('appumWMhUzhgN8fFe');

function main() {
    return { payload: helper() }
}

function helper() {
    return new Date();
}

exports.main = main;

// Returns { "payload": "2021-03-04T20:44:09.084Z"} 

This ran on Cloud Functions without error. I now have to figure out how to get my Airtable functions to work and return the data I’m looking for. I’m close, but still struggling with callbacks. A new post might be coming soon.

Also, I wrote a script to package everything. The * allows me to automatically zip all contents in the folder, which saves a step of typing them out individually in your guidance.

Any time I change my index.js code located in my folder called “airtable-inc-tutorial” and want to re-upload it. I ran this script, called setup.sh, using the command: sh setup.sh

# zip my files together
zip -r function.zip *

# update the ibm cloud function with the new code
sudo ibmcloud fn action update airtable-inc-tutorial function.zip --kind nodejs:10

# invoke the code
sudo ibmcloud fn action invoke airtable-inc-tutorial --result

Even though IBM Cloud Functions has a UI, I couldn’t see a way to upload zip files, but luckily the command to write it is very simple and above. (replace “action update” with “action create” when doing this for the first time).

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.