Trouble using Airtable in NodeJS: 'not defined' error

Hey everyone! I’m working on a NodeJS project using NestJS and I’m trying to get data from Airtable. I installed the AirtableJS library and the DefinitelyTyped definitions with yarn. I’ve set up my repository class to work with Airtable, but when I run the code, I get a ‘ReferenceError: Airtable is not defined’ error.

Here’s a simplified version of what I’m doing:

import { Base } from 'airtable';

class MyRepository {
  private apiKey: string;
  private baseId: string;

  constructor() {
    this.apiKey = 'my-api-key';
    this.baseId = 'my-base-id';
  }

  async fetchData() {
    const base: Base = new Airtable({apiKey: this.apiKey}).base(this.baseId);
    // More code here
  }
}

Am I missing something obvious? Did I forget to import Airtable correctly? Any help would be great. Thanks!

I’ve run into this issue before with Airtable in NodeJS. The problem is likely with how you’re importing Airtable. Instead of importing just the Base type, try importing the entire Airtable object like this:

import Airtable from ‘airtable’;

Then in your fetchData method, you can create the base like this:

const base = new Airtable({apiKey: this.apiKey}).base(this.baseId);

Also, make sure you’ve properly installed the airtable package. You can do this by running:

npm install airtable

or if you’re using yarn:

yarn add airtable

If you’re still having issues after trying these steps, double-check your tsconfig.json file to ensure it’s set up correctly for using external modules. Hope this helps!

hey, looks like u didn’t import correctly. try adding:

import Airtable from ‘airtable’;

at the top. also, check if airtable is installed.

I’ve encountered similar issues when working with Airtable in Node.js. The problem likely stems from how you’re importing Airtable. Instead of importing just the Base type, try importing the entire Airtable object:

import Airtable from ‘airtable’;

Then modify your fetchData method:

const base = new Airtable({apiKey: this.apiKey}).base(this.baseId);

Also, ensure you’ve installed the airtable package correctly. Run ‘npm install airtable’ or ‘yarn add airtable’ depending on your package manager.

If the issue persists, check your tsconfig.json file to confirm it’s properly configured for external modules. Sometimes TypeScript configuration can cause unexpected import issues.