Serverless-offline: version 6.7.0 offline: Failure: Cannot read property 'defineService' of undefined

Created on 27 Aug 2020  Â·  8Comments  Â·  Source: dherault/serverless-offline

Bug Report

Current Behavior
When I version up serverless-offline from 6.6.0 to 6.7.0, and I perform API testing, I start to get the following error.
Everything is ok in 6.6.0.

offline: GET /local/api/v1/session (λ: session)
offline: Failure: Cannot read property 'defineService' of undefined
TypeError: Cannot read property 'defineService' of undefined
    at Object.<anonymous> (/Users/chenj49/git/git_gs-failure-relief-pack/backend/node_modules/aws-sdk/clients/sts.js:7:19)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/chenj49/git/git_gs-failure-relief-pack/backend/node_modules/aws-sdk/lib/credentials/temporary_credentials.js:2:11)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/chenj49/git/git_gs-failure-relief-pack/backend/node_modules/aws-sdk/lib/node_loader.js:32:1)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/chenj49/git/git_gs-failure-relief-pack/backend/node_modules/aws-sdk/clients/all.js:1:1)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/chenj49/git/git_gs-failure-relief-pack/backend/.build/src/nonfunctional/notification.js:43:13)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at Object.<anonymous> (/Users/chenj49/git/git_gs-failure-relief-pack/backend/.build/handler.js:45:22)
    at Module._compile (internal/modules/cjs/loader.js:776:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12)
    at Module.require (internal/modules/cjs/loader.js:683:19)
    at require (internal/modules/cjs/helpers.js:16:16)
    at /Users/chenj49/git/git_gs-failure-relief-pack/backend/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:80:133
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
    at InProcessRunner.run (/Users/chenj49/git/git_gs-failure-relief-pack/backend/node_modules/serverless-offline/dist/lambda/handler-runner/in-process-runner/InProcessRunner.js:80:9)

Sample Code
my command to start serverless offline.

serverless offline --stage local start

Environment

  • serverless version: [v1.79.0]
  • serverless-offline version: [v6.7.0]
  • node.js version: [v12.6.0]
  • OS: [macOS 10.14.6]

Most helpful comment

I found the way to fix this problem.

Just add following settings into your serverless.yml

custom:
  serverless-offline:
    allowCache: true

I found the changes of v6.6.0 → v6.7.0
The default option will clear global variables every time when handler is called, I am using aws-sdk, there are some global variables in this node module.
I think they become to undefined, and occurs this issue.

All 8 comments

I'm running into this same issue today running similar versions and a similar start command:

  • start command: sls offline --stage local --prefix=<my_service_name>
  • serverless - v1.80.0
  • serverless-offline - v6.7.0
  • node.js - 12.x
  • OS - Arch Linux

Got the same issue, downgrading to 6.6.0 resolved my problem temporarily.

Note: All works fine for first request, above error pops up on second and each next one.

serverless - 1.81.0
serverless-offline - v6.7.0
aws-sdk - 2.741.0
node.js - 14.8.0
OS - macOS 10.15.6

I found the way to fix this problem.

Just add following settings into your serverless.yml

custom:
  serverless-offline:
    allowCache: true

I found the changes of v6.6.0 → v6.7.0
The default option will clear global variables every time when handler is called, I am using aws-sdk, there are some global variables in this node module.
I think they become to undefined, and occurs this issue.

I'm not sure if this ticket should be closed. By default - without allowCache: true being set - my app is crashing. I don't think that should be default behaviour. There needs to be a better fix for that problem or allowCache should be default be true.

Adding the allowCache: true option to serverless did not solve the problem for me, but installing a specific combination of aws-sdk, serverless-offline, and serverless packages allowed me to work around the issue. Here are the commands I used in my project that finally ended up working:

  • yarn remove aws-sdk serverless serverless-offline
  • yarn add -D serverless@^1.78.1 [email protected] aws-sdk@^2.741.0

I've been hit by this process as well. I look forward to having a definitive solution! :)

At this moment I can only recommend downgrading to v6.6.0

For me it was missing handler: handler.run

Was this page helpful?
0 / 5 - 0 ratings

Related issues

MEGApixel23 picture MEGApixel23  Â·  4Comments

Rafaelsk picture Rafaelsk  Â·  4Comments

dnalborczyk picture dnalborczyk  Â·  3Comments

stunningpixels picture stunningpixels  Â·  3Comments

aldofunes picture aldofunes  Â·  3Comments