Error because `import.meta.url` is undefined

In order to require older modules, you write this:

import { createRequire } from 'module';
const require = createRequire(import.meta.url)

This works if I run the file with node my-file.js. However, I’ve been using the package netlify-cli to run things locally, and when I do that, it logs an error saying that import.meta.url is undefined:

The argument ‘filename’ must be a file URL object, file URL string, or absolute path string. Received undefined

If you want to see in more detail, this is all open source:

What can I pass to createRequire to make this work?

Is it possible to use import.meta.url in a lambda function?

Thank you

Hey @nichoth,

I agree, what you’re doing seems to be valid. Would you be able to open an issue on the CLI repository so that the developers can directly see it? It could be a bug, as it’s definitely not a highly-used way with Functions.

@nichoth and @hrishikesh could this be because the Node runtime for the lambda is running by default on version ‘12’ rather than on 14?

From what I remember import.meta is polyfilled with an empty object on v12. It’s been awhile.

Set AWS_LAMBDA_JS_RUNTIME = “nodejs14.x” in the Netlify UI admin and see if that changes the outcome you are looking for?

I don’t believe that’s the issue. We switched to Node 14 as the default a while back and all sites that don’t have Node 12 explicitly mentioned are now using Node 14.

And a reference to a new github issue —