question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ES6 Javascript functions are not working with esbuild

See original GitHub issue

Hi,

I converted a few functions to use the es6 syntax. They run fine with “netlify dev” locally but when deployed they throw an error:

{"errorType":"Runtime.UserCodeSyntaxError","errorMessage":"SyntaxError: Unexpected token 'export'","trace":["Runtime.UserCodeSyntaxError: SyntaxError: Unexpected token 'export'","    at _loadUserApp (/var/runtime/UserFunction.js:98:13)","    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)","    at Object.<anonymous> (/var/runtime/index.js:43:30)","    at Module._compile (internal/modules/cjs/loader.js:999:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)","    at Module.load (internal/modules/cjs/loader.js:863:32)","    at Function.Module._load (internal/modules/cjs/loader.js:708:14)","    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:60:12)","    at internal/main/run_main_module.js:17:47"]}

Here is the function that fails:

export const handler = async (event, _context) => {
  const subject = event.queryStringParameters.name || 'World';

  try {
    return {
      statusCode: 200,
      body: JSON.stringify({
        message: `Hello ${subject}`
      })
    };
  } catch (error) {
    return { statusCode: 500, body: error.toString() };
  }
};

In my netlify.toml file I do specify esbuild:

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

I also have a Typescript function and it runs just fine both locally and deployed.

CJS Version (works) - https://scottwalter.netlify.app/.netlify/functions/hello-world?name=Scott Typescript Version (works) - https://scottwalter.netlify.app/.netlify/functions/hello-typescript?type=hotdog ES Version (does not work) - https://scottwalter.netlify.app/.netlify/functions/hello-world-es?name=Scott

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
eduardoboucascommented, Sep 15, 2021

Thanks, @scottfwalter. That additional context was super helpful in pointing us to the right direction. It seems to be a problem with the plugin you mentioned.

@ehmicky from our team has raised an issue in the plugin’s repo. We can keep this one open until there’s a resolution.

0reactions
eduardoboucascommented, Oct 29, 2021

Closing as this is an issue with a plugin, not zip-it-and-ship-it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Babel works esbuild doesn't #1475 - GitHub
When I transpile my React library with babel, it works just fine. It looks like it gets transpiled into cjs. Esbuld also appears...
Read more >
Content Types - ESBuild
TypeScript types are treated as comments and are ignored by esbuild, so TypeScript is treated as "type-checked JavaScript." The interpretation of the type ......
Read more >
esbuild Not Including JS Functions Called by HTML
EDIT: I tried to add "export" on those functions but it didn't work. Is there an option to include in package.json in order...
Read more >
Getting started with esbuild - LogRocket Blog
Learn how to quickly and efficiently bundle TypeScript, React, CSS, and image files in this comprehensive esbuild tutorial.
Read more >
How JavaScript works: A deep dive into esbuild
Extreme speed without needing a cache; ES6 and CommonJS modules ... Although esbuild does not have a robust feature-set like Webpack, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found