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.

sam build: NPM Failed when InlineCode is used

See original GitHub issue

Description:

When I create a template which contains at least one function, which uses InlineCode, I cannot build the project with sam build. It fails with the message:

Build Failed
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../Users/username/path/to/template/folder" as it does not contain a package.json file.

I still can run ‘sam package’, although it requires from me an --s3-bucket parameter to be provided, which makes no sense if I have only functions with InlineCode. I also can deploy with sam deploy ... if there is nothing, which I need to build first in the project.

But if I have a project with a mixture of functions, which have to be built and which have InlineCode, I cannot build it.

Steps to reproduce:

Create empty project with the runtime nodejs12.x, delete all autogenerated JS code and leave only template file with the following content.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  Sample SAM Template for bug report
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: "exports.handler = (event, context, callback) => { return { }; }"
      Handler: index.handler
      Runtime: nodejs12.x

Try to build it with sam build.

Observed result:

The command fails with an error.

Build Failed
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../Users/username/path/to/template/folder" as it does not contain a package.json file.

Expected result:

I expect the command goes through and there are build artefacts generated in .aws-sam folder (in case template has more functions, which have to be built).

Additional environment details (Ex: Windows, Mac, Amazon Linux etc)

  1. OS: macOs mojave
  2. sam --version: SAM CLI, version 1.13.2

Add --debug flag to command you are running


sam build --debug
2020-12-11 11:10:51,822 | 'build' command is called
...
2020-12-11 11:10:51,868 | Building codeuri: . runtime: nodejs12.x metadata: {} functions: ['HelloWorldFunction']
2020-12-11 11:10:51,868 | Building to following folder /Users/xxx/tmp/mimas-test/.aws-sam/build/HelloWorldFunction
2020-12-11 11:10:51,869 | Loading workflow module 'aws_lambda_builders.workflows'
...
2020-12-11 11:10:51,888 | Found workflow 'NodejsNpmBuilder' to support capabilities 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
2020-12-11 11:10:51,888 | Running workflow 'NodejsNpmBuilder'
2020-12-11 11:10:51,888 | Running NodejsNpmBuilder:NpmPack
2020-12-11 11:10:51,888 | NODEJS packaging file:/Users/xxx/tmp/mimas-test to /var/folders/sn/zhmg6dt91f55k1fcd82xplwh0000gn/T/tmp696mt8f_
2020-12-11 11:10:51,888 | executing NPM: ['npm', 'pack', '-q', 'file:/Users/xxx/tmp/mimas-test']
2020-12-11 11:10:52,301 | NodejsNpmBuilder:NpmPack failed
Traceback (most recent call last):
  File "/usr/local/Cellar/aws-sam-cli/1.13.2/libexec/lib/python3.8/site-packages/aws_lambda_builders/workflows/nodejs_npm/actions.py", line 59, in execute
    tarfile_name = self.subprocess_npm.run(["pack", "-q", package_path], cwd=self.scratch_dir).splitlines()[-1]
  File "/usr/local/Cellar/aws-sam-cli/1.13.2/libexec/lib/python3.8/site-packages/aws_lambda_builders/workflows/nodejs_npm/npm.py", line 85, in run
    raise NpmExecutionError(message=err.decode("utf8").strip())
aws_lambda_builders.workflows.nodejs_npm.npm.NpmExecutionError: NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../../../../../../../Users/xxx/tmp/mimas-test" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/xxx/.npm/_logs/2020-12-11T10_10_52_284Z-debug.log

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/Cellar/aws-sam-cli/1.13.2/libexec/lib/python3.8/site-packages/aws_lambda_builders/workflow.py", line 269, in run
    action.execute()
  File "/usr/local/Cellar/aws-sam-cli/1.13.2/libexec/lib/python3.8/site-packages/aws_lambda_builders/workflows/nodejs_npm/actions.py", line 70, in execute
    raise ActionFailedError(str(ex))
aws_lambda_builders.actions.ActionFailedError: NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../../../../../../../Users/xxx/tmp/mimas-test" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/xxx/.npm/_logs/2020-12-11T10_10_52_284Z-debug.log

Build Failed
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../../../../../../../xxx/xxx/tmp/mimas-test" as it does not contain a package.json file.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
maslakovcommented, Feb 6, 2021

Hi, I can confirm in the version 1.17 sam build goes through and deploys inline functions correctly with and without package.json

0reactions
qingchmcommented, Mar 2, 2021

Closing this issue since the InlineCode improvement https://github.com/aws/aws-sam-cli/pull/2549 corrected the behaviour to support InlineCode usage. For anyone who experienced this issue please update the SAMCLI to version 1.16 or above!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - NPM Failed: npm ERR! code Z_DATA_ERROR
Try building this in a container. Use sam build -u. This worked for me, but I am not sure why it wouldn't build...
Read more >
5 Ways To Bundle a Lambda Function Within an AWS CDK ...
So I thought let's create it myself and publish it later to NPM. ... The easiest solution is to write some inline code...
Read more >
Getting started with the AWS SAM CLI - SQLShack
Figure 1 – Writing code within the inline Code Editor (AWS Lambda) ... The SAM is based on the previously built Cloud Formation...
Read more >
In Depth Guide to Serverless APIs with AWS Lambda ... - Moesif
We create a CognitoUserPool object with the help of the environment variables we got from the SAM template and use this object inside...
Read more >
Write A Serverless Function with AWS Lambda and MongoDB
Node.js 12. MongoDB Atlas can be used for FREE with a M0 ...
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