build icon indicating copy to clipboard operation
build copied to clipboard

Cannot instantiate Netlify client in JavaScript with Bun

Open pachico opened this issue 1 year ago • 2 comments

Describe the bug

When I try to create a client with these lines:

// file foo.ts
import { NetlifyAPI } from 'netlify';

try {
    const client = new NetlifyAPI("my-token");
    console.log('Client created successfully');
} catch (error) {
    console.error('Error creating Netlify client:', error);
}

I get the following error

bun run foo.ts 
1 | import omit from 'omit.js';
2 | import { openApiSpec } from './open_api.js';
3 | // Retrieve all OpenAPI operations
4 | export const getOperations = function () {
5 |     return Object.entries(openApiSpec.paths).flatMap(([path, pathItem]) => {
6 |         const operations = omit.default(pathItem, ['parameters']);
                               ^
TypeError: omit.default is not a function. (In 'omit.default(pathItem, ["parameters"])', 'omit.default' is undefined)
      at /[redacted]/node_modules/netlify/lib/operations.js:6:28
      at /[redacted]/node_modules/netlify/lib/index.js:87:24

Steps to reproduce

  1. Create new file foo.ts
  2. Add dependency via bun with bun add netlify
  3. paste content provided above in the file
  4. execute with bun run foo.ts

Configuration

Deploy logs

pachico avatar Apr 29 '24 13:04 pachico

@pachico if this works in Node, then it's a bug in Bun and not in Netlify.

Without reading omit.js's code, my guess is Bun is respecting __esModule on omit.js when converting to ESM, so omit.default is undefined since that's the equivalent of require("omit.js").default.default and that would cause the error "omit.default" is not a function

Jarred-Sumner avatar May 05 '24 08:05 Jarred-Sumner

I just ran into this issue and resolved it by installing the latest LTS of Node.js.

I thought that Bun was all I needed.

jzombie avatar Sep 25 '24 12:09 jzombie