cli
cli copied to clipboard
Error because `import.meta.url` is undefined
Describe the bug
In order to require older modules, I've been writing 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:
https://github.com/nichoth/ssc-server/blob/1965d193da893bb98e98bd9962717d65c32a4f83/netlify/functions/profile/profile.js#L14
This is relevant to this post
Steps to reproduce
Either
Use a lambda function that includes import.meta.url
, and run it with netlify-cli
or
clone my example at this commit within git :
https://github.com/nichoth/ssc-server/blob/1965d193da893bb98e98bd9962717d65c32a4f83/netlify/functions/profile/profile.js#L14
Then run npm start
, which will run netlify dev
Configuration
[build]
command = "npm run build"
functions = "netlify/functions"
publish = "public"
## more info on configuring this file: https://www.netlify.com/docs/netlify-toml-reference/
[dev]
autoLaunch = false
publish = "public" # Folder with the static content for _redirect file
[template.environment]
FAUNADB_SERVER_SECRET = "faunaDB secret"
CLOUDINARY_API_SECRET = "cloudinary secret"
CLOUDINARY_CLOUD_NAME="cloudinary cloud name"
CLOUDINARY_API_KEY="cloudinary API key"
PUBLIC_KEY = "public key"
SECRET_KEY = "secret key"
[functions]
node_bundler = "esbuild"
[functions.profile]
external_node_modules = ["@nichoth/ssc"]
Environment
System:
OS: macOS 12.3.1
CPU: (8) arm64 Apple M1
Memory: 70.31 MB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.1 - ~/.nvm/versions/node/v16.13.1/bin/node
Yarn: 1.22.18 - ~/.nvm/versions/node/v16.13.1/bin/yarn
npm: 8.3.0 - ~/.nvm/versions/node/v16.13.1/bin/npm
npmPackages:
netlify-cli: ^10.3.0 => 10.3.0
npmGlobalPackages:
netlify-cli: 9.16.6
@nichoth did you ever resolve this? Running ESM with import.meta.url is giving me the same error on AWS Lambda 16x
@alexcroox
No actually, I think I refactored so that everything is using require
on the server.
I don't blame you. The node ecosystem still isn't ready for ESM without a lot of pain during the intial stages of setting up a new project...
For any other weary travellers I solved this on AWS by using import dirname from 'es-dirname'
.
dirname()
is a hack that throws an error and reads the file path from the error stack!
We do not yet support pure ESM modules in netlify functions. Currently, it will all be compiled to commonjs.
Supporting this is on our roadmap though. Ref: https://github.com/netlify/zip-it-and-ship-it/issues/750
Would love to see support for this, especially with Node 18 and Web API adoption on the backend picking up steam, using import.meta.url
+ URL
would be great to have.