Skip to content

Missing dependency request causes an error when using Google Cloud's Vision API #212

@tferrerm

Description

@tferrerm

Environment details

  • OS: Ubuntu 20.04 x64
  • Node.js version: v14.2.0
  • npm version: 7.24.1
  • google-gax version: 2.28.0

Steps to reproduce

  1. Create a serverless Node.js project
  2. Add the serverless-webpack plugin to your serverless.yml file and use webpack with babel to package the function that will be created
  3. Add @google-cloud/vision as a dependency
  4. Create a Node.js Google Function or AWS Lambda that makes use of the Google Vision API (e.g. do something similar to what is shown in this SafeSearch example)
  5. Attempt to build the lambda with webpack

Here's an example YML configuration file for a Lambda that uses this:

service: ${file(./config/global.json):SERVICE_NAME}
frameworkVersion: "2"

plugins:
  - serverless-webpack
  - serverless-prune-plugin

provider:
  name: aws
  runtime: nodejs14.x
  stage: ${opt:stage, 'dev'}
  # Examples loading from a local config file. Utilizes stage flag when invoked. If no --stage was passed into invocation, use the "dev" configuration
  region: ${file(./config/${opt:stage, 'dev'}.json):REGION}
  stackName: ${file(./config/${opt:stage, 'dev'}.json):IDENTIFER_BASE}-cfStack-stackname

custom:
  webpack:
    packager: "yarn"
    includeModules:
      forceExclude:
        - aws-sdk
  somethingTopicARN:
    Fn::ImportValue: 'trust-sns-${opt:stage,self:provider.stage}-something'
  somethingRoleARN:
    Fn::ImportValue: 'trust-sns-${opt:stage,self:provider.stage}-something-Role'
  anotherthingTopicARN:
    Fn::ImportValue: 'trust-sns-${opt:stage,self:provider.stage}-anotherthing'

package:
  individually: true
  patterns:
    - '!node_modules/@aws-sdk/**'
    - '!node_modules/aws-sdk/**'
    - '!node_modules/pretty-format/**'
    - '!node_modules/typescript/**'
    - '!node_modules/@typescript-eslint/**'
    - '!node_modules/prettier/**'
    - '!node_modules/pretty-format/**'
    - '!node_modules/@babel/**'

    - '!tests/**'
    - '!sns-serverless.yml'
    - '!s3-temp-trigger-serverless.yml'
    - '!coverage/**'
    - '!.circleci/**'
    - '!README.md'

functions:
  vertexApi:
    handler: src/something-service/handler/lambdaGcloudWorker.handler
    name: trust-use1-${opt:stage, 'dev'}-lambda-vertex
    role: ${self:custom.somethingRoleARN}
    memorySize: 1024
    timeout: 900
    maximumRetryAttempts: 0
    environment:
      NODE_ENV: ${file(./config/${opt:stage, 'dev'}.json):STAGE}
      s3PreImageBucket: 
        Ref: PreProcessingBucket
    events:
      - sns: 
          arn: { "Fn::Join" : ["", ["${self:custom.somethingTopicARN}"] ]  }
          topicName: 'trust-use1-${opt:stage,self:provider.stage}-sns-something'

  anotherVertexApi:
    handler: src/another-service/handler/lambdaWorker.handler
    name: trust-use1-${opt:stage, 'dev'}-lambda-vertex-2
    role: ${self:custom.imageModerationRoleARN}
    memorySize: 1024
    timeout: 900
    maximumRetryAttempts: 0
    environment:
      NODE_ENV: ${file(./config/${opt:stage, 'dev'}.json):STAGE}
      s3PreImageBucket: 
        Ref: PreProcessingBucket
    events:
      - sns: 
          arn: { "Fn::Join" : ["", ["${self:custom.anotherTopicARN}"] ]  }
          topicName: 'trust-use1-${opt:stage,self:provider.stage}-sns-anotherthing'

# you can add CloudFormation resource templates here
resources:
  Resources:
    PreProcessingBucket:
      Type: AWS::S3::Bucket
      Properties:
        BucketName: 'trust-use1-${opt:stage,self:provider.stage}-s3'
        AccessControl: Private
        PublicAccessBlockConfiguration:
          BlockPublicAcls: true
          BlockPublicPolicy: true
          IgnorePublicAcls: true
          RestrictPublicBuckets: true        
        BucketEncryption:
          ServerSideEncryptionConfiguration:
            - ServerSideEncryptionByDefault:
                SSEAlgorithm: AES256
        LifecycleConfiguration:
          Rules:
            -
              ExpirationInDays: 1
              Status: Enabled
              Id: "Deleted in 1 Day"
              NoncurrentVersionExpirationInDays: 1


  Outputs:        

    S3ImageBucket:
      Value:
        Ref: PreProcessingBucket
      Export:
        Name: trust-s3-${opt:stage,self:provider.stage}-vertex

Error

WARNING in ./node_modules/retry-request/index.js 74:21-39
Module not found: Error: Can't resolve 'request' in '/home/user/project/node_modules/retry-request'
 @ ./node_modules/@google-cloud/vision/node_modules/google-gax/build/src/streamingCalls/streaming.js 22:21-45
 @ ./node_modules/@google-cloud/vision/node_modules/google-gax/build/src/index.js 64:18-55
 @ ./node_modules/@google-cloud/vision/build/src/helpers.js 24:12-33
 @ ./node_modules/@google-cloud/vision/build/src/index.js 31:16-36
 @ ./src/image-service/handler/lambdaGcloudWorker.js 38:15-46

Possible solution

Add the package request as a dependency of the project here given you've included retry-request

Workaround

Add request as a dependency of the serverless project by running npm install request --save

Also: Issue #353 was closed for no good reason and we had lots of further comments of people with the same bug.
The request library has reached end of life so the ideal solution would be to get rid of it completely in this project.

Metadata

Metadata

Assignees

No one assigned

    Labels

    priority: p2Moderately-important priority. Fix may not be included in next release.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions