Unexpected token 'export'

Hi guys,
I deployed my first sveltekit project here but, sometimes, when I visit the page https://shorturl.at/dHIVZ I get this error. I don’t know how to resolve the problem because this error not happens everytime. I understood that it’s caused by lightgallery but the project is based on this package and I prefer to handle this error instead of choosing another package. I modified the package.json as the error told me but nothing is changed.

site-name: sparkling-palmier-30acfb
Complete error:
Jul 19, 07:23:01 PM: 9af9d8a2 ERROR (node:8) Warning: To load an ES module, set “type”: “module” in the package.json or use the .mjs extension.
(Use node --trace-warnings ... to show where the warning was created)Jul 19, 07:23:01 PM: 9af9d8a2 ERROR /var/task/node_modules/lightgallery/plugins/thumbnail/lg-thumbnail.es5.js:482
export default Thumbnail;
^^^^^^

SyntaxError: Unexpected token ‘export’
at internalCompileFunction (node:internal/vm:73:18)
at wrapSafe (node:internal/modules/cjs/loader:1178:20)
at Module._compile (node:internal/modules/cjs/loader:1220:27)
at Module._extensions…js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at ModuleWrap. (node:internal/modules/esm/translators:169:29)
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)Jul 19, 07:23:01 PM: 9af9d8a2 ERROR Unhandled Promise Rejection {“errorType”:“Runtime.UnhandledPromiseRejection”,“errorMessage”:“SyntaxError: Unexpected token ‘export’”,“reason”:{“errorType”:“SyntaxError”,“errorMessage”:“Unexpected token ‘export’”,“stack”:[“/var/task/node_modules/lightgallery/plugins/thumbnail/lg-thumbnail.es5.js:482”,“export default Thumbnail;”,“^^^^^^”,“”,“SyntaxError: Unexpected token ‘export’”," at internalCompileFunction (node:internal/vm:73:18)“,” at wrapSafe (node:internal/modules/cjs/loader:1178:20)“,” at Module._compile (node:internal/modules/cjs/loader:1220:27)“,” at Module._extensions…js (node:internal/modules/cjs/loader:1310:10)“,” at Module.load (node:internal/modules/cjs/loader:1119:32)“,” at Module._load (node:internal/modules/cjs/loader:960:12)“,” at ModuleWrap. (node:internal/modules/esm/translators:169:29)“,” at ModuleJob.run (node:internal/modules/esm/module_job:194:25)“]},“promise”:{},“stack”:[“Runtime.UnhandledPromiseRejection: SyntaxError: Unexpected token ‘export’”,” at process. (file:///var/runtime/index.mjs:1186:17)“,” at process.emit (node:events:513:28)“,” at emit (node:internal/process/promises:149:20)“,” at processPromiseRejections (node:internal/process/promises:283:27)“,” at process.processTicksAndRejections (node:internal/process/task_queues:96:32)"]}

Thart’s happening because Netlify Functions don’t support ESM yet. Internally, we bundle your code as CJS. But looks like your code is somewhere still using ESM:

Thus the issue. Try adding a package.json with the content:

{
  "type": "module"
}

anywhere in your site and copy that to your functions like:

[functions]
  included_files = ["path-to-this-package.json"]

Though I’m not sure what other effects would that have,

Additionally, you can also switch to Edge Functions by changing your SvelteKit config. You can check that in their docs.

Hi @hrishikesh, thanks for your response and sorry of the delay of mine. I resolved the problem by downloading the repo and selecting manually files like lightgallery.es5.js.

Hi thanks for coming back and sharing your solution with the community!