floydspace / serverless-esbuild

💨 A Serverless framework plugin to bundle JavaScript and TypeScript with extremely fast esbuild
MIT License
451 stars 138 forks source link

Unable to pass option to npm install #115

Closed exoego closed 3 years ago

exoego commented 3 years ago

Hello.

On NPM v7+, my package.json requires npm install --legacy-peer-deps command to be executed, instead of plain npm install. However, it seems that serverless-esbuild currently does not support passing options to npm install: https://github.com/floydspace/serverless-esbuild/blob/753e4aede6e826adc77cf82847520f1f84f9bef8/src/packagers/npm.ts#L104-L109 So, sls deploy fails due to npm installation failure.

It would be helpful if there is an option to passing arguments to npm install something like:

custom:
  esbuild:
    installExtraArgs:
      - "--legacy-peer-deps"

Error message

$ sls deploy
Serverless: Compiling with esbuild...
Serverless: Compiling completed.
Serverless: Package lock found - Using locked versions
Serverless: Packing external modules: aws-appsync@^4.0.3, @react-native-community/async-storage, @react-native-community/netinfo, graphql-tag@^2.11.0, graphql

 Error ---------------------------------------------------

  Error: npm install failed with code 1
      at ChildProcess.<anonymous> (/Users/cw-tatsuno/IdeaProjects/sagrada-appsync-poc/backend/issue/node_modules/serverless-esbuild/dist/utils.js:48:24)
      at ChildProcess.emit (events.js:315:20)
      at ChildProcess.EventEmitter.emit (domain.js:467:12)
      at maybeClose (internal/child_process.js:1048:16)
      at Socket.<anonymous> (internal/child_process.js:439:11)
      at Socket.emit (events.js:315:20)
      at Socket.EventEmitter.emit (domain.js:467:12)
      at Pipe.<anonymous> (net.js:673:12)

     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.

  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com

  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.16.0
     Framework Version:         2.35.0 (local)
     Plugin Version:            4.5.3
     SDK Version:               4.2.2
     Components Version:        3.8.3

Reproduction

  1. Setup files like below.
  2. npm install --legacy-peer-deps
  3. sls deploy

Files

serverless.yml

service: issue-repro

plugins:
  - serverless-esbuild

custom:
  esbuild:
    external:
      - 'aws-appsync'
      - 'graphql-tag'

provider:
  name: aws
  region: ap-northeast-1
  runtime: nodejs14.x
  memorySize: 256
  versionFunctions: false
  lambdaHashingVersion: 20201221

functions:
  helloworld:
    handler: src/index.helloworld
    events: []

package.json

{
  "name": "issue-repro",
  "version": "0.1.0",
  "devDependencies": {
    "@types/aws-lambda": "^8.10.75",
    "@types/node": "^14.14.41",
    "serverless": "^2.35.0",
    "serverless-esbuild": "^1.10.2",
    "ts-node": "^9.1.1",
    "typescript": "~4.2.4"
  },
  "dependencies": {
    "aws-appsync": "^4.0.3",
    "graphql-tag": "^2.11.0"
  },
  "scripts": {}
}

index.ts

import type {MSKHandler} from 'aws-lambda';

import gql from 'graphql-tag';
import AWSAppSyncClient from 'aws-appsync';

const appSyncClient = new AWSAppSyncClient({
    auth: {...},
    region: "...", 
    url: "..."
});

const helloworld: MSKHandler = async (event) => {
    const query = gql(`mutation Test($message: String!){
      create(message: $message){
        id
        message
      }
    }`);

    const params =  {
        "message": "TEST"
    }

    try {
        await appSyncClient.mutate({
            variables: params,
            mutation: query
        });
        console.log("Success.");
    } catch (err) {
        console.error(JSON.stringify(err));
        throw new Error("Mutation failed !!")
    }
};

export {
    helloworld
};
floydspace commented 3 years ago

hi @exoego, thank you for the issue and PR. it seems like a nice to have feature

exoego commented 3 years ago

FYI: I have built on this branch and confirmed the npm error gone.

github-actions[bot] commented 3 years ago

:tada: This issue has been resolved in version 1.21.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: