aws-sam-cli icon indicating copy to clipboard operation
aws-sam-cli copied to clipboard

sam build: NPM Failed when InlineCode is used

Open maslakov opened this issue 5 years ago • 6 comments

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.

maslakov avatar Dec 11 '20 10:12 maslakov

Faced the same issue when trying to build my project locally using docker container and my lambda runtime is python. Detailed debug logs of my build failure:

Using the request object from command line argument
Loading workflow module 'aws_lambda_builders.workflows'
Registering workflow 'PythonPipBuilder' with capability 'Capability(language='python', dependency_manager='pip', application_framework=None)'
Registering workflow 'NodejsNpmBuilder' with capability 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Registering workflow 'RubyBundlerBuilder' with capability 'Capability(language='ruby', dependency_manager='bundler', application_framework=None)'
Registering workflow 'GoDepBuilder' with capability 'Capability(language='go', dependency_manager='dep', application_framework=None)'
Registering workflow 'GoModulesBuilder' with capability 'Capability(language='go', dependency_manager='modules', application_framework=None)'
Registering workflow 'JavaGradleWorkflow' with capability 'Capability(language='java', dependency_manager='gradle', application_framework=None)'
Registering workflow 'JavaMavenWorkflow' with capability 'Capability(language='java', dependency_manager='maven', application_framework=None)'
Registering workflow 'DotnetCliPackageBuilder' with capability 'Capability(language='dotnet', dependency_manager='cli-package', application_framework=None)'
Registering workflow 'CustomMakeBuilder' with capability 'Capability(language='provided', dependency_manager=None, application_framework=None)'
Found workflow 'NodejsNpmBuilder' to support capabilities 'Capability(language='nodejs', dependency_manager='npm', application_framework=None)'
Running workflow 'NodejsNpmBuilder'
Running NodejsNpmBuilder:NpmPack
NODEJS packaging file:/tmp/samcli/manifest to /tmp/samcli/scratch
executing NPM: ['npm', 'pack', '-q', 'file:/tmp/samcli/manifest']
NodejsNpmBuilder:NpmPack failed
Traceback (most recent call last):
  File "/usr/local/opt/sam-cli/lib64/python3.7/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/opt/sam-cli/lib64/python3.7/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 "../manifest" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/opt/sam-cli/lib64/python3.7/site-packages/aws_lambda_builders/workflow.py", line 269, in run
    action.execute()
  File "/usr/local/opt/sam-cli/lib64/python3.7/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 "../manifest" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log
Builder workflow failed
Traceback (most recent call last):
  File "/usr/local/opt/sam-cli/lib64/python3.7/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/opt/sam-cli/lib64/python3.7/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 "../manifest" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/opt/sam-cli/lib64/python3.7/site-packages/aws_lambda_builders/workflow.py", line 269, in run
    action.execute()
  File "/usr/local/opt/sam-cli/lib64/python3.7/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 "../manifest" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/opt/sam-cli/lib64/python3.7/site-packages/aws_lambda_builders/__main__.py", line 126, in main
    mode=params.get("mode", None),
  File "/usr/local/opt/sam-cli/lib64/python3.7/site-packages/aws_lambda_builders/builder.py", line 125, in build
    return workflow.run()
  File "/usr/local/opt/sam-cli/lib64/python3.7/site-packages/aws_lambda_builders/workflow.py", line 76, in wrapper
    func(self, *args, **kwargs)
  File "/usr/local/opt/sam-cli/lib64/python3.7/site-packages/aws_lambda_builders/workflow.py", line 276, in run
    raise WorkflowFailedError(workflow_name=self.NAME, action_name=action.NAME, reason=str(ex))
aws_lambda_builders.exceptions.WorkflowFailedError: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../manifest" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log
2020-12-30 16:19:01,207 | Build inside container returned response {"jsonrpc": "2.0", "id": 1, "error": {"code": 400, "message": "NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL\nnpm ERR! Could not install from \"../manifest\" as it does not contain a package.json file.\n\nnpm ERR! A complete log of this run can be found in:\nnpm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log"}}

Build Failed
2020-12-30 16:19:01,474 | Sending Telemetry: {'metrics': [{'commandRun': {'awsProfileProvided': False, 'debugFlagProvided': True, 'region': '', 'commandName': 'sam build', 'duration': 256368, 'exitReason': 'BuildInsideContainerError', 'exitCode': 1, 'requestId': 'b3ee592d-fe78-47f6-a681-4d602797b96c', 'installationId': 'a3d3433a-912c-43a9-ba45-27275030acf5', 'sessionId': '1aa872be-8797-472f-a641-d1224db5104b', 'executionEnvironment': 'CLI', 'pyversion': '3.9.0', 'samcliVersion': '1.15.0'}}]}
2020-12-30 16:19:02,531 | HTTPSConnectionPool(host='aws-serverless-tools-telemetry.us-west-2.amazonaws.com', port=443): Read timed out. (read timeout=0.1)
Error: NodejsNpmBuilder:NpmPack - NPM Failed: npm ERR! code ENOLOCAL
npm ERR! Could not install from "../manifest" as it does not contain a package.json file.

npm ERR! A complete log of this run can be found in:
npm ERR!     /root/.npm/_logs/2020-12-30T10_49_01_168Z-debug.log

mj-roychowdhury avatar Dec 30 '20 10:12 mj-roychowdhury

Thanks for reporting this. Are you removing the package.json file too?

mgrandis avatar Feb 04 '21 21:02 mgrandis

Which package.json are you talking about? As I described above, to reproduce it, you don't even need any function code apart from yaml template, since the code is Inlined there, so there is no package.json file and nothing to delete.

maslakov avatar Feb 06 '21 00:02 maslakov

I thought you were starting off a Node template and removing the unnecessary files, that's why I asked if you were also removing the package.json:

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

I just tested with 1.17.0 and the build succeeded, can you try updating?

We released improvements to the InlineCode handling in 1.16.0.

mgrandis avatar Feb 06 '21 01:02 mgrandis

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

maslakov avatar Feb 06 '21 11:02 maslakov

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!

qingchm avatar Mar 02 '21 19:03 qingchm