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

Cannot use node-fetch 3 in serverless functions #4558

Open
whitep4nth3r opened this issue Apr 21, 2022 · 7 comments
Open

Cannot use node-fetch 3 in serverless functions #4558

whitep4nth3r opened this issue Apr 21, 2022 · 7 comments
Labels
type: bug code to address defects in shipped code

Comments

@whitep4nth3r
Copy link
Contributor

whitep4nth3r commented Apr 21, 2022

Describe the bug

Using node-fetch 3 (latest) causes serverless functions to error and not run.

Steps to reproduce

  1. Install latest node-fetch (version 3+)
  2. Attempt to use fetch in a serverless function using import fetch from "node-fetch"
  3. Observe below error:
{"errorMessage":"Cannot use import statement outside a module","errorType":"SyntaxError","level":"error","stackTrace":["ort fetch from \"node-fetch\";","^^^","","taxError: Cannot use import statement outside a module","Object.compileFunction (node:vm:352:18)","wrapSafe (node:internal/modules/cjs/loader:1027:15)","Module._compile (node:internal/modules/cjs/loader:1063:27)","Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)","Module.load (node:internal/modules/cjs/loader:975:32)","Function.Module._load (node:internal/modules/cjs/loader:822:12)","Module.require (node:internal/modules/cjs/loader:999:19)","require (node:internal/modules/cjs/helpers:102:18)","Object._executeSync (/opt/homebrew/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:277:26)","/opt/homebrew/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:95:26"]}

Note: Using node-fetch 2 and importing the dependency via const fetch = require("node-fetch"); works as expected.

Configuration

[[redirects]]
from = "/api/*"
to = "/.netlify/functions/:splat"

Environment

System:
OS: macOS 12.3.1
CPU: (10) arm64 Apple M1 Pro
Memory: 2.33 GB / 32.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 17.9.0 - /opt/homebrew/bin/node
Yarn: 1.22.18 - /opt/homebrew/bin/yarn
npm: 8.5.5 - /opt/homebrew/bin/npm
npmGlobalPackages:
netlify-cli: 10.0.0

@whitep4nth3r whitep4nth3r added the type: bug code to address defects in shipped code label Apr 21, 2022
@whitep4nth3r
Copy link
Contributor Author

Closing because the fix is to name the serverless function with the .mjs extension.

@whitep4nth3r
Copy link
Contributor Author

I've reopened the issue as I think we need some communication around this on the docs, OR, compile .js files that are using the import syntax correctly under the hood.

@jacklyn-net
Copy link

Spoke to @whitep4nth3r briefly about this during the DX Mobbing meeting, and I wanted to surface this for @klavavej to see if this is something that needs to be added to the Functions docs, since this is something that seems to be causing a lot of issues.

@klavavej
Copy link

Spoke to @whitep4nth3r briefly about this during the DX Mobbing meeting, and I wanted to surface this for @klavavej to see if this is something that needs to be added to the Functions docs, since this is something that seems to be causing a lot of issues.

this is somewhat related to https://github.com/netlify/docs/issues/1402
I'd like to see what engineering thinks about fixing this bug before trying to address this pain point with a docs update

@hrishikesh-k
Copy link

I believe if you use:

[functions]
  node_bundler = "esbuild"

it will fix the error and you can directly use .js files. To me, this doesn't appear to be a bug, as we need folks to use esbuild to import ESM modules.

@whitep4nth3r
Copy link
Contributor Author

Agreed @hrishikesh-k — I guess I didn't find good docs about this at the time. We have a blog post out now — but just wanted to follow up with @klavavej as to whether this is now clearer on the docs?

@klavavej
Copy link

klavavej commented Aug 9, 2022

@hrishikesh-k @whitep4nth3r - support for ESM in the default zisi bundler was added in netlify/zip-it-and-ship-it#1018

Since folks are now seeing that node_bundler = "esbuild" is required for .js functions using ESM, I suspect there's been a regression with the feature enhancement to support ESM regardless of the bundler.

https://github.com/netlify/pod-compute/issues/147 has been filed to look into the possible regression

Related docs issue https://github.com/netlify/docs/issues/1760

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug code to address defects in shipped code
Projects
None yet
Development

No branches or pull requests

4 participants