Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error because import.meta.url is undefined #4601

Closed
nichoth opened this issue May 9, 2022 · 6 comments
Closed

Error because import.meta.url is undefined #4601

nichoth opened this issue May 9, 2022 · 6 comments
Labels
area: functions type: bug code to address defects in shipped code

Comments

@nichoth
Copy link

nichoth commented May 9, 2022

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 nichoth added the type: bug code to address defects in shipped code label May 9, 2022
@alexcroox
Copy link

@nichoth did you ever resolve this? Running ESM with import.meta.url is giving me the same error on AWS Lambda 16x

@nichoth
Copy link
Author

nichoth commented Jul 3, 2022

@alexcroox
No actually, I think I refactored so that everything is using require on the server.

@alexcroox
Copy link

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!

@danez
Copy link
Contributor

danez commented Oct 18, 2022

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: netlify/zip-it-and-ship-it#750

@thescientist13
Copy link

thescientist13 commented Apr 16, 2023

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.

@serhalp
Copy link
Contributor

serhalp commented Jul 16, 2024

I believe this should have been resolved a year or two ago! Feel free to reopen if you're still having issues.

@serhalp serhalp closed this as completed Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: functions type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

6 participants