Sam build fails with: Build Failed Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code Z_DATA_ERROR
Asked Answered
D

5

7

I'm learning how to create lambdas on aws. I'm following this official tutorial.

The commands I run:

sam init (default values for everything, I'm choosing template 8 - webapp backend)

cd sam-app

sam build

The output I'm getting is:

Building codeuri: /home/sam-app runtime: nodejs14.x metadata: {} functions: ['getAllItemsFunction', 'getByIdFunction', 'putItemFunction']
Running NodejsNpmBuilder:NpmPack

Build Failed
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code Z_DATA_ERROR
npm ERR! errno -3
npm ERR! zlib: invalid distance too far back

npm ERR! A complete log of this run can be found in:
npm ERR!     /home/username/.npm/_logs/2021-05-20T14_02_59_425Z-debug.log

node version: 16.2.0

npm version: 7.13.0

sam version: 1.23.0

zlib version: 1:1.2.11-4

I'm not modifying any files - they're all generated by sam.

Dermatologist answered 20/5, 2021 at 14:19 Comment(3)
Please help to share your template fileChemistry
did you find a way to solve your problem?Dysphasia
I've stopped using amazon sam, and started using serverlessDermatologist
C
9

Try building this in a container. Use sam build -u

This worked for me, but I am not sure why it wouldn't build without this.

Concettaconcettina answered 24/6, 2021 at 9:28 Comment(2)
It's the worst. It works... but why? This keeps me up at night.Liberati
I dont know why but it works, thanks xdPostulate
E
5

I was having the same issue. I fixed it by the npm init command inside the lambda folder that we create for individual lambda functions. This will fix the version by creating the package.json.

Emulsify answered 7/3, 2022 at 5:54 Comment(0)
C
3

I had the same problem in wsl2, I have solved it by installing a newer version of nodejs(v14.17.1) with nvm https://learn.microsoft.com/en-us/windows/dev-environment/javascript/nodejs-on-wsl https://github.com/nvm-sh/nvm#git-install

Catnip answered 29/6, 2021 at 13:11 Comment(1)
This worked for me. I'm assuming that I had incompatible version of node and npm. Running nvm install node updated both to the latest version.Otiose
C
0

Maybe in your template file, you might be using the below type

Type: AWS::Lambda::Function

If that's the case then change the type to below one

Type: AWS::Serverless::Function

Chemistry answered 20/5, 2021 at 18:40 Comment(0)
B
0

I suspect you're affected by https://github.com/aws/aws-sam-cli/issues/3965 which in turn is probably related to a zlib 1.2.11 problem that should be fixed by updating to use zlib 1.2.12

I've encountered this on both Fedora 36 and Arch Linux.

As Mark suggested you could probably work around it by using a container for the build. This is done by adding -u to the args for sam build.

Depending on which distro you're using you may be able to update zlib.

Note that if you are on Fedora the zlib update may take a while:

Byyourleave answered 18/6, 2022 at 11:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.