Aws-sam-cli: Doesn't work with npm scripts

Created on 27 Jan 2018  路  7Comments  路  Source: aws/aws-sam-cli

Given a simple project:

_index.js_

exports.handler = (event, context, callback) => {
  callback(null, {
      body: "Hello World!"
    });
}

_template.yml_

AWSTemplateFormatVersion : '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  HelloWorld:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      Events:
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

_package.json_

{
  "name": "npm-sam-test",
  "scripts": { "start": "sam local start-api" },
  "devDependencies": { "aws-sam-local": "^0.2.6" }
}

I would expect the follow in to work:

$ npm install
$ npm start

Instead I get the following error:

PS C:\Users\robpc\project\npm-sam-test> npm start

> npm-sam-test@ start C:\Users\robpc\project\npm-sam-test
> sam local start-api

'sam' is not recognized as an internal or external command,
operable program or batch file.
[...]

I am not not an expert on npm publishing, but it looks like there is some missing information in the package.json to identify the binary as it is not in the project node_modules/.bin.

Most helpful comment

I appreciate the workaround, but my intention in filing an issue as opposed to say using StackOverflow was to request this standard npm behavior be supported. There are many situations where installing to global is not desired.

As a by the way, a better workaround (in that it addresses this usage pattern) would be something like the below, except it is not platform agnostic and requires the user to reverse engineer the package.

{
  "name": "npm-sam-test",
  "scripts": { "start": ".\node_modules\aws-sam-local\node_modules\.bin\sam.exe local start-api" },
  "devDependencies": { "aws-sam-local": "^0.2.6" }
}

NPM provides mechanisms for this usage and they are implemented in many other packages, so I don't think it is an unreasonable request.

All 7 comments

have you tried install sam local as a global I.e. npm install -g aws-sam-local

I appreciate the workaround, but my intention in filing an issue as opposed to say using StackOverflow was to request this standard npm behavior be supported. There are many situations where installing to global is not desired.

As a by the way, a better workaround (in that it addresses this usage pattern) would be something like the below, except it is not platform agnostic and requires the user to reverse engineer the package.

{
  "name": "npm-sam-test",
  "scripts": { "start": ".\node_modules\aws-sam-local\node_modules\.bin\sam.exe local start-api" },
  "devDependencies": { "aws-sam-local": "^0.2.6" }
}

NPM provides mechanisms for this usage and they are implemented in many other packages, so I don't think it is an unreasonable request.

I've added a postinstall step to work around this. It allows sam to function as expected in npm scripts. But this does not work across platforms either.

"postinstall": "ln -fs `pwd`/node_modules/aws-sam-local/node_modules/.bin/sam ./node_modules/.bin/sam",

If bin field is added to aws-sam-local/distribution/npm/package.json, "scripts": { "start": "sam local start-api" } works correctly?

{
  "bin": {
    "sam": "node_modules/.bin/sam"
  }
}

Related: https://docs.npmjs.com/files/package.json#bin

Same issue here. I'd like to be able to use this with a non-global install to simplify use across developers and different environments. We generally discourage global npm installs; they're difficult to manage, keep up to date, and can open some development machines to security issues over time.

To work around this I wrote a little bin/sam wrapper in my local project that does the following:

#!/usr/bin/env bash
set -e
node_modules/aws-sam-local/node_modules/.bin/sam $*

Not relevant anymore since v0.3.0 moved to PIP

Was this page helpful?
0 / 5 - 0 ratings