build
build copied to clipboard
Cannot instantiate Netlify client in JavaScript with Bun
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
- Create new file
foo.ts - Add dependency via bun with
bun add netlify - paste content provided above in the file
- execute with
bun run foo.ts
Configuration
Deploy logs
@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
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.