dherault / serverless-offline

Emulate AWS λ and API Gateway locally when developing your Serverless project
MIT License
5.2k stars 794 forks source link

process.env.IS_OFFLINE #1568

Closed jwillingham789 closed 2 years ago

jwillingham789 commented 2 years ago

It would be great if the IS_OFFLINE env variable were either optional or could be given a custom name. I'm currently having an env collision issue with another library. When i delete the following line in the code everything works as I want it to.

https://github.com/dherault/serverless-offline/blob/7416c1cd9251efb37fba62941bd48e769c7ab18c/src/lambda/LambdaFunction.js#L115

dnalborczyk commented 2 years ago

thank you for filing the issue @jwillingham789

we could certainly do that. that said, I would be interested to know on how this causes issues, as the IS_OFFLINE env var is only visible in the lambda handler itself (when running with worker threads, the default). is the "other library" another serverless plugin, or is that a library you are using imported by your handler code?

jwillingham789 commented 2 years ago

@dnalborczyk The other library is imported by my handler code. The problematic code in the other library is here:

https://github.com/alixaxel/chrome-aws-lambda/blob/f9d5a9ff0282ef8e172a29d6d077efc468ca3c76/source/index.ts#L180

I really just want to unset the IS_OFFLINE variable completely. Thanks again for your help!

dnalborczyk commented 2 years ago

thanks for the reference. it looks like the code was added to fix something with serverless-offline:

PR: https://github.com/alixaxel/chrome-aws-lambda/pull/195 issue: https://github.com/alixaxel/chrome-aws-lambda/issues/194

I personally don't think it's a good idea to check for that IS_OFFLINE variable in any project. the best option for you might be to create an issue or a PR removing that code altogether.

btw, I tried to add a chrome-aws-lambda scenario test case for another (similar) issue https://github.com/dherault/serverless-offline/issues/918 but I must have forgotten. the mentioned repro project did work tho when updated to everything latest.

dnalborczyk commented 2 years ago

@jwillingham789 I'll try to add a scenario test soon. in the meanwhile, you could just remove the IS_OFFLINE variable from the process.env, or set it to undefined in your handler:

delete process.env.IS_OFFLINE
// or
process.env.IS_OFFLINE = undefined

export async function handler() {
  return {
    ....
  }
}

edit: forgot the env in process.env.IS_OFFLINE . also forgot async keyword in handler function.

jwillingham789 commented 2 years ago

@dnalborczyk Thanks again for your help. I'll use that code for now!.

dnalborczyk commented 2 years ago

@jwillingham789 I added a working test case: https://github.com/dherault/serverless-offline/tree/master/tests/scenario/lambda-frameworks/sparticuz-chrome-aws-lambda

if you go to that directory you should be able to deploy with serverless as well as running serverless-offline locally. this is using a fork as the original does not seem to be maintained anymore.

I'll add another for httpApi a bit later as well.

closing this for good house keeping.