You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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:
Run npm init svelte@next my-app && cd my-app && npm install
Install netlify-adapter for sveltekit: npm install --save-dev @sveltejs/adapter-netlify@next
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
Connect netlify-cli to local repo and netlify site to use netlify dev
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
Run netlify functions:create test and select the "Hello World" option
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:
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.
The text was updated successfully, but these errors were encountered:
…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>
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...
Describe the bug
When running
netlify dev
withsvelte@next
(sveltekit), local lambda functions respond with 500 error. Would appear thatnetlify-cli/src/lib/functions/netlify-function.js
is usinglambda-local/build/lambdalocal.js
to issue a CommonJS require of the function file but fails when"type"="module"
exists in the root levelpackage.json
file as this tells Node to treat all.js
files as ES modules.To Reproduce
Steps to reproduce the behavior:
npm init svelte@next my-app && cd my-app && npm install
netlify-adapter
for sveltekit:npm install --save-dev @sveltejs/adapter-netlify@next
svelte.config.js
to utilize new adapter:netlify dev
netlify.toml
in root and add these rules:netlify functions:create test
and select the "Hello World" optionConfiguration
Expected behavior
Expect the local functions structure to support
"type"="module"
in package.jsonCLI Output
Additional context
Add any other context about the problem here.
The text was updated successfully, but these errors were encountered: