In attempting to create a function that uses the Netlify API I have this:
import { NetlifyAPI } from "netlify"
const client = new NetlifyAPI(process.env.NETLIFY_API_KEY)
const handler = async function(event, context) {
/**
* Trigger a rebuild using the API
*/
const response = await client.createSiteBuild({ site_id: process.env.SITE_ID })
return {
statusCode: 200,
};
};
module.exports.handler = handler;
Regardless of trying to run this locally, or when deployed, I receive the error
TypeError: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
This happens with the import (I tested removing the const client and const response lines and the same error was still thrown.) Looking at this thread it would appear use of NetlifyAPI in a function is possible (I’ve tried require('netlify') with the same result.
If I create a script.js with similar code and run node script.js everything works fine. Am I missing something—is using NetlifyAPI like this not possible? Am I doing something not quite right—is the implementation in a function different than a script?
Package is netlify@11.0.0. The idea behind this is to use it as a scheduled function to trigger a rebuild periodically. Is there a better way to do this?
While this gets investigated, I think you can use node fetch or Axios to accomplish the same task. You simply need to make a call to that API endpoint with the auth token as the authorization header in bearer token.
I’m having this issue, almost a year on. Has there been no progress?
The solution isn’t really a solution at all - it’s just a complete refactoring of the code.
Using Fetch works, but I’d really like to use the API client as it’ll keep things neat and tidy and take a lot of work away.
I don’t know of any updates, since I can only see the same GitHub issue as you and can see no updates there myself
I’ll ask our team to review and suggest if they have any more expedient workarounds or a suggestion for you; I imagine we’ll get a chance to talk with them next week so we may not have their feedback for 10 days or so, just to set expectations.
To be clear and make sure we ask the appropriate questions on your behalf, could you specify exactly which of the things that we talked about here you are running into?
I’m trying to create a Netlify Function which uses the Netlify API, so it would make sense for things to work when done per the documentation. It’s a bit confusing that the Netlify API client is useable in any Node application other than one hosted by Netlify!
All it takes to give the error is this:
import { NetlifyAPI } from "netlify"
When trying to import netlify, the error comes from part of the Node package (open_ai.js) and crashes the function.
TypeError: The argument 'filename' must be a file URL object, file URL string, or absolute path string. Received undefined
new NodeError (node:internal/errors:393:5)
createRequire (node:internal/modules/cjs/loader:1278:11)
Object. (***/netlify/functions/sites/node_modules/netlify/lib/open_api.js:4:17)
Module._compile (node:internal/modules/cjs/loader:1159:14)
Object.Module._extensions..js (node:internal/modules/cjs/loader:1213:10)
Module.load (node:internal/modules/cjs/loader:1037:32)
Function.Module._load (node:internal/modules/cjs/loader:878:12)
Module.require (node:internal/modules/cjs/loader:1061:19)
require (node:internal/modules/cjs/helpers:103:18)
Object. (/***/.netlify/functions-serve/sites/sites.js:1:18)
To reproduce, you can make a completely empty Netlify site, install netlify, and put that single import into a function. The error occurs locally or when deployed.
This error doesn’t occur when using other approaches (like fetching the API myself), but the API client is there to make things neater and easier for API developers, so I’m reluctant to start building without it.