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

Netlify Dev functions 500 error with package.json type="module" #3394

Closed
srobertson421 opened this issue Sep 24, 2021 · 1 comment · Fixed by #3445
Closed

Netlify Dev functions 500 error with package.json type="module" #3394

srobertson421 opened this issue Sep 24, 2021 · 1 comment · Fixed by #3445
Assignees
Labels
area: command: dev area: functions type: bug code to address defects in shipped code

Comments

@srobertson421
Copy link

Describe the bug

When running netlify dev with svelte@next (sveltekit), local lambda functions respond with 500 error. Would appear that netlify-cli/src/lib/functions/netlify-function.js is using lambda-local/build/lambdalocal.js to issue a CommonJS require of the function file but fails when "type"="module" exists in the root level package.json file as this tells Node to treat all .js files as ES modules.

To Reproduce

Steps to reproduce the behavior:

  1. Run npm init svelte@next my-app && cd my-app && npm install
  2. Install netlify-adapter for sveltekit: npm install --save-dev @sveltejs/adapter-netlify@next
  3. Change svelte.config.js to utilize new adapter:
/** @type {import('@sveltejs/kit').Config} */
import adapter from '@sveltejs/adapter-netlify';
 
const config = {
  kit: {
    adapter: adapter(),
    // hydrate the <div id="svelte"> element in src/app.html
    target: '#svelte'
	}
};
 
export default config
  1. Connect netlify-cli to local repo and netlify site to use netlify dev
  2. Create netlify.toml in root and add these rules:
[build]
  command = "npm run build"
  publish = "build"

[functions]
  directory = "functions"
  node_bundler = "esbuild"

[dev]
  command = "npm run dev"
  targetPort = 3000
  port = 5000

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

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
  force = false
  1. Run netlify functions:create test and select the "Hello World" option
  2. Attempt to invoke the function from either the cli or from the svelte application.

Configuration

[build]
  command = "npm run build"
  publish = "build"

[functions]
  directory = "functions"
  node_bundler = "esbuild"

[dev]
  command = "npm run dev"
  targetPort = 3000
  port = 5000

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

[[redirects]]
  from = "/*"
  to = "/index.html"
  status = 200
  force = false
  • Please enter the following command in a terminal and copy/paste its output:
System:
    OS: Linux 5.11 Ubuntu 21.04 (Hirsute Hippo)
    CPU: (4) x64 Intel(R) Core(TM) i5-7300U CPU @ 2.60GHz
    Memory: 616.22 MB / 7.65 GB
    Container: Yes
    Shell: 5.8 - /usr/bin/zsh
  Binaries:
    Node: 16.10.0 - ~/.nvm/versions/node/v16.10.0/bin/node
    npm: 7.24.0 - ~/.nvm/versions/node/v16.10.0/bin/npm
  npmGlobalPackages:
    netlify-cli: 6.9.15

Expected behavior

Expect the local functions structure to support "type"="module" in package.json

CLI Output

Request from ::1: GET /.netlify/functions/test
{"level":"error","message":"End - Error:"}
{"errorMessage":"require() of ES Module /home/sean/code/kit-test/functions/test/test.js from /home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js not supported.\ntest.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which declares all .js files in that package scope as ES modules.\nInstead rename test.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change \"type\": \"module\" to \"type\": \"commonjs\" in /home/sean/code/kit-test/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).\n","errorType":"Error","stackTrace":["t.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains \"type\": \"module\" which declares all .js files in that package scope as ES modules.","tead rename test.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change \"type\": \"module\" to \"type\": \"commonjs\" in /home/sean/code/kit-test/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).","","Object._executeSync (/home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:277:26)","/home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:95:26","new Promise (<anonymous>)","Object.execute (/home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/node_modules/lambda-local/build/lambdalocal.js:87:16)","Object.invokeFunction (/home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/src/lib/functions/runtimes/js/index.js:57:36)","NetlifyFunction.invoke (/home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/src/lib/functions/netlify-function.js:76:41)","async handler (/home/sean/.nvm/versions/node/v16.10.0/lib/node_modules/netlify-cli/src/lib/functions/server.js:103:33)"],"level":"error"}
Response with status 500 in 46 ms.

Additional context

Add any other context about the problem here.

@srobertson421 srobertson421 added the type: bug code to address defects in shipped code label Sep 24, 2021
@netlify-team-account-1 netlify-team-account-1 self-assigned this Oct 4, 2021
@kodiakhq kodiakhq bot closed this as completed in #3445 Oct 14, 2021
kodiakhq bot pushed a commit that referenced this issue Oct 14, 2021
…3445)

* feat: write reproduction test for #3394

* Update serving-functions.test.js

* feat: fix by using import() for lambda funcs

* chore: use esm: true for another test

* feat: switch import/require based on Node version

* chore: don't cache dynamic imports

* fix: different approach

trying to use "import" was surprisingly hard.
I learned from @eduardoboucas that we use esbuild (via ZISI) to transpile
ESM to CJS.
This commit improves detection for that, and makes sure that any upper-level
"package.json: type=module" directive is overriden.

* fix: optional chaining syntax unsupported

* chore: use existing fs abstraction

* chore: add explanatory comment

* chore: rename variable

Co-authored-by: Netlify Team Account 1 <netlify-team-account-1@users.noreply.github.com>
@maddsua
Copy link

maddsua commented Mar 29, 2023

I don't think that it's resolved. Still getting a similar error, the only difference is that in my case CLI just dumps the entire error trace like this Error: require() of ES Module.... not supported...

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

Successfully merging a pull request may close this issue.

4 participants