如何在Serverless.yml中使NODE_ENV值转大写且保留stage原小写状态?
How to Convert NODE_ENV to Uppercase While Keeping Stage Parameter Lowercase in Serverless Framework
Problem Statement
I want to reuse my serverless.yml configuration across dev, test, and prod environments. Currently, my config looks like this:
provider: name: aws stage: ${opt:stage, 'dev'} environment: NODE_ENV: ${self:provider.stage}
Right now, NODE_ENV matches the lowercase stage value (dev/test/prod). I need to keep the input stage parameter and self:provider.stage in lowercase, but convert only the NODE_ENV value to uppercase.
Solution
Absolutely! The Serverless Framework supports built-in string manipulation functions directly in variable references, so you can achieve this with a tiny tweak to your configuration:
provider: name: aws stage: ${opt:stage, 'dev'} environment: NODE_ENV: ${self:provider.stage.toUpperCase()}
Explanation
- Your
stageparameter stays lowercase (whether you pass it via--stageor use the default 'dev')—we don’t modify that original value at all. - The
self:provider.stage.toUpperCase()call takes the existing lowercase stage value and converts it to uppercase only for theNODE_ENVenvironment variable. - For example:
- Running
serverless deploy --stage testwill keepself:provider.stageastest, but setNODE_ENVtoTEST - Using the default
devstage will result inNODE_ENV: DEV
- Running
This keeps your configuration clean and reusable while meeting your requirement of distinct casing for the stage parameter vs. NODE_ENV.
内容的提问来源于stack exchange,提问作者Arek Jaworski
相关产品推荐
相关产品推荐

