netlify / build

Netlify Build (node process) runs the build command, Build Plugins and bundles Netlify Functions. Can be run in Buildbot or locally using Netlify CLI
https://docs.netlify.com/configure-builds/build-plugins/
MIT License
215 stars 54 forks source link

Cannot instantiate Netlify client in JavaScript with Bun #5613

Open pachico opened 2 months ago

pachico commented 2 months ago

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

Jarred-Sumner commented 2 months ago

@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