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

Doesn't work with npm scripts

Open robpc opened this issue 8 years ago • 7 comments

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.

robpc avatar Jan 27 '18 19:01 robpc

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

pal-philbasford avatar Jan 27 '18 20:01 pal-philbasford

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.

robpc avatar Jan 27 '18 20:01 robpc

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",

jumoel avatar Feb 13 '18 09:02 jumoel

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

yami-beta avatar Feb 27 '18 01:02 yami-beta

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.

ebickle avatar Mar 26 '18 16:03 ebickle

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 $*

metaskills avatar Mar 31 '18 18:03 metaskills

Not relevant anymore since v0.3.0 moved to PIP

sanathkr avatar Jun 01 '18 00:06 sanathkr