AWS SAM - Esbuild Failed: cannot find esbuild
Asked Answered
K

5

8

I'm trying out AWS SAM with nodeJS and Typescript using 'sam init', but when I want to build the example hello-world-typescript application using 'sam build', I get the following message:

Building codeuri: C:\RF\GitHub\rf-09-06\hello-world runtime: nodejs14.x metadata: {'BuildMethod': 'esbuild', 'BuildProperties': {'Minify': True, 'Target': 'es2020', 'Sourcemap': True, 'EntryPoints': ['app.ts']}} architecture: x86_64 functions: ['HelloWorldFunction']
Running NodejsNpmEsbuildBuilder:CopySource
Running NodejsNpmEsbuildBuilder:NpmInstall
Running NodejsNpmEsbuildBuilder:EsbuildBundle

Build Failed
Error: NodejsNpmEsbuildBuilder:EsbuildBundle - Esbuild Failed: cannot find esbuild

Any idea how to resolve it?

Thanks!

Kling answered 9/6, 2022 at 9:7 Comment(1)
I would suggest using @vercel/ncc for a less disruptive build process. You can keep the node_modules and other files like package.json as is in their original location, instead of moving stuff around.Stiffen
P
19

It can be solved in either of the 2 ways:

  1. Install esbuild globally using npm install -g esbuild

  2. As mentioned in the AWS SAM github issues, move esbuild to non-dev dependency (npm install esbuild) - Since esbuild will bundle your code, it won't be packaged in your Lambda.

PS: latest version of hello-world-typescript has this already corrected as mentioned in point 2.

Parasitism answered 14/9, 2022 at 13:51 Comment(4)
This should be the preferred answer!Caresa
This actually solved the problem for me. [AWS CodeBuild]Chiles
Neither solution works. Upon trying to install esbuild globally, the install.js file under esbuild can't be found within the global node_modules folder. Using NVM results in same, just within the nvm global node_modules folder. Trying to install locally results in same error, within the local node_modules folder.Mettah
I was exhausted on trying the 1st option, thanks for the 2nd, it workedFibriform
H
6

I had to install esbuild globally to get this working npm install -g esbuild

Hanks answered 4/7, 2022 at 15:40 Comment(1)
BEWARE: This fixes the problem but it might introduce inconsistencies among versions of esbuild. Please, refer the @Parasitism for better explanationCaresa
E
2

I was encountering this issue as well. I put sam build as an npm script so that it looks at your project's node_modules.

package.json

{
  ...
  "scripts": {
    "build": "sam build"
  }
}

This way you don't have to install esbuild globally. Then just npm run build or yarn build

Euchre answered 25/4, 2023 at 18:31 Comment(0)
B
1

install esbuild, you can use npm for that

npm install esbuild
Blister answered 10/6, 2022 at 8:58 Comment(2)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Autum
It has been installed, but still no luck. What else it can be?Kling
G
0

You can install esbuild as development dependency and run sam build in package manager (ex: pnpm) scope

example:

pnpm sam build

Glennaglennie answered 24/9, 2022 at 8:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.