0

I am given this task to perform some stuff with data obtained from an AirTable API with NodeJS. I am using the AirTableJS node library with the DefinitelyTyped airtable definitions (I am using NestJS).

So, naturally, I do the following to import the necessary packages.

yarn add airtable @types/airtable

Then, in my repository that interacts with the AirTable API, I have the following.

import { User } from "../entities/user";
import { ConfigService } from "@nestjs/config";
import { Inject, Injectable } from "@nestjs/common";
import { LogService } from "src/log/services/log/log.service";
import { Base } from "airtable";


@Injectable()
export class UsersRepository {
    private readonly apiKey: string;
    private readonly baseId: string;

    constructor(@Inject(ConfigService) private readonly config: ConfigService, @Inject(LogService) private readonly log: LogService) {
        this.apiKey = this.config.get<string>('airtable.apiKey');
        this.baseId = this.config.get<string>('airtable.baseId');
    }

    async getUnmatchedUsers(): Promise<User[]> {

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

But, when running it, I get the following error relating to the repository function:

ReferenceError: Airtable is not defined

Am I missing anything here or did I not import the Airtable package correctly?

Thanks.

Patrick Lumenus
  • 1,412
  • 1
  • 15
  • 29

3 Answers3

1

It's not defined because you haven't imported Airtable.

That's probably going to look like:

import Airtable, { Base } from "airtable";
Alex Wayne
  • 178,991
  • 47
  • 309
  • 337
  • I tried your solution. And, it doesn't seem to like me importing Airtable directly as it gices the error: ```Module "" can only be default-imported using the 'esModuleInterop' flag```. – Patrick Lumenus Apr 29 '20 at 20:02
  • You should set `"esModuleInterop": true,` in your `tsconfig.json` file. Lots of npm modules require it to make default import work. And apparently airtable is one of them. [More info here](https://stackoverflow.com/questions/56238356/understanding-esmoduleinterop-in-tsconfig-file) – Alex Wayne Apr 29 '20 at 20:06
  • Alternatively, you could try `import * as Airtable from 'airtable'` or `const Airtable = require('airtable')`. But `esModuleInterop` really shouldn't cause you any trouble. – Alex Wayne Apr 29 '20 at 20:11
  • Okay. Thanks. That works. I guess I should start setting esModuleInterop to true in all my projects to make stuff easier. :) – Patrick Lumenus Apr 29 '20 at 20:13
1

Currently using Airtable in a Typescript project (tsconfig below) and the following allowed me to make api calls against my Airtable base.

Node v16.5.1 Typescript v4.7.4 Airtable v0.11.4

import Airtable from 'airtable';

Airtable.configure({
  endpointUrl: "https://api.airtable.com",
  apiKey: `${process.env.AIRTABLE_API_KEY}`,
});
const base = Airtable.base(AIRTABLE_BASE);

base('myTableKey')
  .select({ 
     fields: [
       'fieldA', 'fieldB'
     ]
  })
{
  "compilerOptions": {
    // Enable top-level await, and other modern ESM features.
    "target": "ESNext",
    "module": "ESNext",
    // Enable node-style module resolution, for things like npm package imports.
    "moduleResolution": "node",
    // Enable JSON imports.
    "resolveJsonModule": true,
    // Enable stricter transpilation for better output.
    "isolatedModules": true,
    // Add type definitions for our Vite runtime.
    "types": ["node","vite/client"],
    "outDir": "dist",
    "allowSyntheticDefaultImports": true
  }
}
0

You don't have the Airtable class imported from anywhere, so Node and Tyepscript don't have any idea what it is. The closest you have is the Base type imported from the airtable pacakge. As their documentation isn't public, it's hard to say how to fix it.

Jay McDoniel
  • 57,339
  • 7
  • 135
  • 147