# Problem building lambda function

**URL:** <https://answers.netlify.com/t/problem-building-lambda-function/44460>\
**Category:** Support\
**Tags:** lambda-functions\
**Created:** [September 21, 2021, 2:04pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460 "2021-09-21T14:04:38Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 21, 2021, 2:04pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/1 "2021-09-21T14:04:38Z")

</div>

I am getting the error:

```auto
 npm run build:lambda

> marvel@0.1.0 build:lambda
> netlify-lambda build src/utilities/lambda --config ./webpack.lambda.js

netlify-lambda: Building functions
[
  ModuleParseError: Module parse failed: Unexpected character '#' (48:1)
  You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
  |
  | /** @type {Array.<(Blob|Uint8Array)>} */
  > #parts = [];
  | #type = '';
  | #size = 0;

```

I’m using create-react-app. I also have an extra webpack file “webpack.lambda.js” I run on lambda build:

```auto
module.exports = {
  optimization: {minimize: false},
  devtool: "source-map",
  target: "node",
}

```

---

<div class="post-metadata">

**Author:** ![hillary](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hillary/32/21132_2.png) [@hillary](https://answers.netlify.com/u/hillary)\
**Post date:** [September 21, 2021, 3:31pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/2 "2021-09-21T15:31:00Z")

</div>

Hey there, @ShaCP 👋

Thanks for reaching out. Can you please share your site name and your function name?

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 21, 2021, 4:00pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/4 "2021-09-21T16:00:40Z")

</div>

Hi hillary (noted about PMs, just didn’t want to post the info you asked here)

Looks like the issue was a newer version of the node-fetch package. Reverting back to 2.6.1 fixed this issue. Still don’t know why it doesn’t work with the newer version though.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 22, 2021, 3:48am UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/5 "2021-09-22T03:48:03Z")

</div>

Hi @ShaCP,

Any specific reason why you’re using `netlify lambda` instead of letting Netlify bundle functions automatically using `zisi` or `esbuild`?

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 22, 2021, 12:40pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/6 "2021-09-22T12:40:33Z")

</div>

@hrishikesh Because I wanted to use syntax such as “import”.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 22, 2021, 1:01pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/7 "2021-09-22T13:01:43Z")

</div>

You can do it using `esbuild`.

`netlify.toml`:

```auto
[functions]
  node_bundler = "esbuild"

```

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 30, 2021, 12:40am UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/8 "2021-09-30T00:40:01Z")

</div>

Ok, I have switched to esbuild. I’m not sure about the configuration though.

With netlify-lambda, I followed the docs and this is what I did:

netlify.toml:

```auto
[build]
command = "npm run build"
functions = "lambda"
publish = "build"

```

package.json

```auto
    "build:lambda": "netlify-lambda build src/utilities/lambda --config ./webpack.lambda.js",
    "debug": "netlify dev --inspect"

```

webpack.lambda.js

```auto
module.exports = {
  optimization: {minimize: false},
  devtool: "source-map",
  target: "node",
}

```

This all worked for me as explained in the netlify-lambda docs.

How can I do this with esbuild? I removed from all the code everything that is netlify-lambda specific to make sure I didn’t have unnecessary code. (including `functions = "lambda" `, I don’t see a `function` property mentioned outside of the netlify-lambda docs.

Then I added:

```auto
[functions]
node_bundler = "esbuild"

```

According to the docs:

> The following property applies for all Netlify Functions:
> 
> directory: a custom path to your serverless functions. The default location is YOUR\_BASE\_DIRECTORY/netlify/functions

So I added this to netlify.toml under functions

```auto
directory = "src/utilities/lambda/"

```

With this config, Netlify Dev works. But it won’t let me step through the code like when I used netlify-lambda. A source map issue?

This was my vscode configuration that worked with netlify-lambda?

```auto
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Netlify Debugging",
            "type": "pwa-node",
            "request": "launch",
            "program": "${workspaceFolder}\\node_modules\\.bin\\netlify",
            "runtimeArgs": [
                "run-script",
                "debug"
            ],
            "runtimeExecutable": "npm",
            "skipFiles": [
                "<node_internals>/**"
            ],
            "sourceMapPathOverrides": {
                "webpack:///./*": "${workspaceFolder}/src/utilities/lambda/*",
            },
            "resolveSourceMapLocations": [
                "${workspaceFolder}/**",
                "! **/node_modules/**"
            ]
        }
    ]
}

```

What do I need to change?

Also, although the function is working with Netlify Dev, it’s not being included in the production build. “npm run build” doesn’t build the function.

Lastly, just FYI when I run netlify dev, the function is being built here:  
.netlify\functions-serve\apiRequest\src\netlify\functions\

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 30, 2021, 10:43am UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/9 "2021-09-30T10:43:16Z")

</div>

> [@ShaCP](#):
>
> How can I do this with esbuild?

There’s nothing you need to do. Once you’ve specified `node_bundler` as `esbuild` and stored your function file in the folder you specified, Netlify and Netlify Dev will automatically parse it and bundle your functions. There’s no additional build step that you need. You just need to build your website, functions are automatically handled.

> [@ShaCP](#):
>
> With this config, Netlify Dev works. But it won’t let me step through the code like when I used netlify-lambda. A source map issue?

I haven’t tried it myself, but I believe you could add breakpoints in the file generated in the `.netlify` folder in your local folder structure.

> [@ShaCP](#):
>
> “npm run build” doesn’t build the function.

`npm run build` won’t. But `netlify build` will include the bundled functions.

> [@ShaCP](#):
>
> the function is being built here

That is correct.

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 30, 2021, 3:10pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/10 "2021-09-30T15:10:02Z")

</div>

Ok, so I’m using netlify build. I am now running into an issue with my environment variables. They are undefined. They work fine using netlify dev but not on the deployment preview. They were working fine when I was using netlify-lambda. I have them defined in my netlify.toml

```auto
environment = { REACT_APP_PUBLIC_API_KEY="00000", REACT_APP_PRIVATE_API_KEY="00000" }

```

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 30, 2021, 3:22pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/11 "2021-09-30T15:22:13Z")

</div>

Are these environment variables being returned undefined in Functions? if yes, that is expected behaviour. Environment variables set in `netlify.toml` are not accessible during runtime. You need to set them in the UI.

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 30, 2021, 3:38pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/12 "2021-09-30T15:38:28Z")

</div>

Yes, they are on netlify functions. Are sure about this though? My current live deploy (which, again, uses netlify-lambda) works fine and they are set on netlify.toml. Is there something about netlify-lambda that can work with env vars from netlify.toml?

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [September 30, 2021, 4:01pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/13 "2021-09-30T16:01:50Z")

</div>

I personally can’t comment on that as I’ve never used `netlify-lambda`, maybe it consumes the environment variables and inlines them, something like this plugin: [netlify-plugin-inline-functions-env - npm](https://www.npmjs.com/package/netlify-plugin-inline-functions-env)

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 30, 2021, 6:22pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/14 "2021-09-30T18:22:05Z")

</div>

I still can’t get the breakpoints to work. Hopefully someone can help me out with this. Setting breakpoints on the file you mentioned didn’t work, and it’s not ideal either because it’s a transformed file.

I also tried the config here:  
[https://cli.netlify.com/vscode/](https://cli.netlify.com/vscode/)

```auto
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "netlify dev",
      "type": "node",
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "outFiles": ["${workspaceFolder}/.netlify/functions-serve/**/*.js"],
      "program": "${workspaceFolder}/node_modules/.bin/netlify",
      "args": ["dev"],
      "console": "integratedTerminal",
      "env": { "BROWSER": "none" },
      "serverReadyAction": {
        "pattern": "Server now ready on (https?://[\w:.-]+)",
        "uriFormat": "%s",
        "action": "debugWithChrome"
      }
    },
    {
      "name": "netlify functions:serve",
      "type": "node",
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "outFiles": ["${workspaceFolder}/.netlify/functions-serve/**/*.js"],
      "program": "${workspaceFolder}/node_modules/.bin/netlify",
      "args": ["functions:serve"],
      "console": "integratedTerminal"
    }
  ]
}

```

And that didn’t work at all. I’m getting this error:

```auto
Debugger attached.
Waiting for the debugger to disconnect...
F:\some\path\node_modules\.bin\netlify:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
          ^^^^^^^

SyntaxError: missing ) after argument list
    at wrapSafe (internal/modules/cjs/loader.js:1001:16)
    at Module._compile (internal/modules/cjs/loader.js:1049:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1114:10)
    at Module.load (internal/modules/cjs/loader.js:950:32)
    at Function.Module._load (internal/modules/cjs/loader.js:790:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:76:12)
    at internal/main/run_main_module.js:17:47

```

---

<div class="post-metadata">

**Author:** ![ShaCP](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/shacp/32/28321_2.png) [@ShaCP](https://answers.netlify.com/u/ShaCP)\
**Post date:** [September 30, 2021, 9:01pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/15 "2021-09-30T21:01:44Z")

</div>

I was actually able to get breakpoints working using the file inside .netlify. So that’s some progress. Thanks @hrishikesh. I just had to remove the commented code from the launch.json I was using with netlify-lambda:

```auto
{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Netlify Debugging",
            "type": "pwa-node",
            "request": "launch",
            "program": "${workspaceFolder}\\node_modules\\.bin\\netlify",
            "runtimeArgs": [
                "run-script",
                "debug"
            ],
            "runtimeExecutable": "npm",
            "skipFiles": [
                "<node_internals>/**"
            ],
           // "sourceMapPathOverrides": {
           // "webpack:///./*": "${workspaceFolder}/src/utilities/lambda/*",
            },
            "resolveSourceMapLocations": [
                "${workspaceFolder}/**",
                "! **/node_modules/**"
            ]
        }
    ]
}

```

Still would like to know if I can step through my original file with the original syntax rather than a transformed file. And also would still like to know why the launch.json config I mentioned in the post above isn’t working for me.

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [October 1, 2021, 3:37am UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/16 "2021-10-01T03:37:46Z")

</div>

> [@ShaCP](#):
>
> Still would like to know if I can step through my original file

The file is going to be transformed in the end, so I personally think it’s better to debug that file. Also, `esbuild` produces a source map so, I believe that it should be able to show you your original code too - that’s how it works in browsers, I haven’t debugged a lot of code inside VSCode itself.

About the second question of yours, again I’m sorry I have no idea - I’m not a VSCode-based debugger (have literally done it just once or twice on a standalone node program). From reading the JSON file and literally analysing properties for what they might be doing, it looks like it should have worked as the JSON file you’ve posted now doesn’t seem anything like it.

---

<div class="post-metadata">

**Author:** ![paulrudy](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/paulrudy/32/41292_2.png) [@paulrudy](https://answers.netlify.com/u/paulrudy)\
**Post date:** [November 2, 2022, 4:32am UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/17 "2022-11-02T04:32:46Z")

</div>

Did you ever learn the answers to these questions? I’m running into the same issues.

---

<div class="post-metadata">

**Author:** ![SamO](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/samo/32/41847_2.png) [@SamO](https://answers.netlify.com/u/SamO)\
**Post date:** [November 2, 2022, 3:17pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/18 "2022-11-02T15:17:08Z")

</div>

Hey @paulrudy , I saw you posted another functions question on a different thread ([process.env.URL undefined in netlify functions:serve](https://answers.netlify.com/t/process-env-url-undefined-in-netlify-functions-serve/78482)). Are these two related?

If you are still encountering obstacles with building a Lambda function please share your site, functions file as well as debugging steps you have taken to troubleshoot.

---

<div class="post-metadata">

**Author:** ![paulrudy](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/paulrudy/32/41292_2.png) [@paulrudy](https://answers.netlify.com/u/paulrudy)\
**Post date:** [November 2, 2022, 5:38pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/19 "2022-11-02T17:38:07Z")

</div>

H @SamO, the other post is unrelated.

My question here is not related to any specific site or functions file. It’s about utilizing debugging in VSCode with Netlify functions, which doesn’t seem to work without pointing the debugger to the _compiled_ (`/.netlify`) js files (as @ShaCP [mentioned](https://answers.netlify.com/t/problem-building-lambda-function/44460/15), not the working files in `/netlify/functions`.

When running the `netlify functions:serve` or `netlify dev` via the VSCode debugger, the debugger complains when I try to set a breakpoint on any function:

> Unbound breakpoint
> 
> Some of your breakpoints could not be set. If you’re having an issue, you can troubleshoot your launch configuration.

When I click the link for troubleshooting, VSCode responds:

> We couldn’t find a corresponding source location, and didn’t find any source with the name \<whatever-function-i-try.js\>

My `launch.json` file is as follows:

```auto
{
  "version": "0.2.0",
  "configurations": [
    {
      "name": "netlify dev",
      "type": "node",
      "runtimeExecutable": "pnpm",
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "outFiles": ["${workspaceFolder}/.netlify/functions-serve/**/*.js"],
      "program": "/Users/paul/Library/pnpm/netlify",
      "args": ["dev"],
      "console": "integratedTerminal",
      "env": { "BROWSER": "none" },
      "serverReadyAction": {
        "pattern": "Server now ready on (https?://[w:.-]+)",
        "uriFormat": "%s",
        "action": "debugWithChrome"
      }
    },
    {
      "name": "netlify functions:serve",
      "type": "node",
      "runtimeExecutable": "pnpm",
      "request": "launch",
      "skipFiles": ["<node_internals>/**"],
      "outFiles": ["${workspaceFolder}/.netlify/functions-serve/**/*.js"],
      "program": "/Users/paul/Library/pnpm/netlify",
      "args": ["functions:serve"],
      "console": "integratedTerminal"
    }
  ]
}

```

Netlify is successfully compiling .js files from `/netlify/functions` to `./netlify`, and the `.js.map` files are included.

In VSCode’s article on Debugging Node.js, it offers [some tips](https://code.visualstudio.com/docs/nodejs/nodejs-debugging#_javascript-source-map-tips)

One of these tips says:

> - Are the `sourceRoot` and `sources` properties in your source map correct? Can they be combined to get the correct path to the `.ts` file?

When I examine any of the compiled scripts’ maps, I see something like: ` "sourceRoot": "/var/folders/6g/w2bsbbjx70xfg899ggx8807r0000gn/T/tmp-9299-XxdxGovpqVVm",`. This clearly isn’t pointing to `/netlify/functions`. Could that be the issue?

---

<div class="post-metadata">

**Author:** ![paulrudy](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/paulrudy/32/41292_2.png) [@paulrudy](https://answers.netlify.com/u/paulrudy)\
**Post date:** [November 3, 2022, 7:06pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/20 "2022-11-03T19:06:28Z")

</div>

Any further info on VSCode debugging of Netlify functions?

---

<div class="post-metadata">

**Author:** ![hrishikesh](https://sea1.discourse-cdn.com/netlify/user_avatar/answers.netlify.com/hrishikesh/32/74607_2.png) [@hrishikesh](https://answers.netlify.com/u/hrishikesh)\
**Post date:** [November 6, 2022, 1:33pm UTC](https://answers.netlify.com/t/problem-building-lambda-function/44460/22 "2022-11-06T13:33:09Z")

</div>

I just tried debugging and it seems to work fine. As soon as I went to: `localhost:8888/.netlify/functions/hello/`, VSCode automatically broke the execution here:

 ![image](https://global.discourse-cdn.com/netlify/original/3X/9/d/9dd6ef25a9fd4e333abe3bb12e0ffd45392d5dc3.png)

here’s the repo I used: [https://github.com/Hrishikesh-K/vscode-test](https://github.com/Hrishikesh-K/vscode-test) and the documentation I followed: [Debugging with Visual Studio Code](https://cli.netlify.com/vscode)

[Next page](https://answers.netlify.com/t/problem-building-lambda-function/44460.md?page=2)
