netlify / zip-it-and-ship-it

Intelligently prepare Node.js Lambda functions for deployment
https://www.npmjs.com/package/@netlify/zip-it-and-ship-it
MIT License
316 stars 35 forks source link

Instantiation of Netlify API client breaks Netlify TS functions #989

Open mfanuzzi opened 2 years ago

mfanuzzi commented 2 years ago

- Do you want to request a feature or report a bug? a bug

- What is the current behavior? importing this library into a Netlify typescript function causes a fatal runtime error

- If the current behavior is a bug, please provide the steps to reproduce.

Create a file called hello.ts in your functions folder:

import { Handler } from '@netlify/functions';
import { NetlifyAPI } from 'netlify';

const handler: Handler = async (event, context) => {
  const client = new NetlifyAPI('test') as any; // <-- this is the line that breaks it

  return {
    statusCode: 200,
    body: JSON.stringify({ message: 'Hello World' }),
  };
};

export { handler };

Then run netlify dev and visit that endpoint; you'll get a 500:

TypeError: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
  createRequire (internal/modules/cjs/loader.js:1186:11)
  Object.<anonymous> (/home/mfan/projects/acb001-crm/node_modules/netlify/src/open_api.js:5:17)
  Module._compile (internal/modules/cjs/loader.js:1085:14)
  Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
  Module.load (internal/modules/cjs/loader.js:950:32)
  Function.Module._load (internal/modules/cjs/loader.js:790:14)
  Module.require (internal/modules/cjs/loader.js:974:19)
  require (internal/modules/cjs/helpers.js:92:18)
  Object.<anonymous> (/home/mfan/projects/acb001-crm/.netlify/functions-serve/hello/hello.js:1:18)
  Module._compile (internal/modules/cjs/loader.js:1085:14)

- What is the expected behavior? The client instantiates

- Please mention your node.js, and operating system version. Node 14, with:

    "netlify": "^10.1.2",
    "@netlify/functions": "^0.11.0",
    "netlify-cli": "^8.15.1",
ehmicky commented 2 years ago

Hi @mfanuzzi,

It seems like import.meta.url in this line of code is undefined.

This seems this bug might not be due to js-client, but to the bundling process done on TypeScript Functions, so I am transferring this to the repository related to it.

cc @eduardoboucas @netlify-team-account-1