You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在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 stage parameter stays lowercase (whether you pass it via --stage or 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 the NODE_ENV environment variable.
  • For example:
    • Running serverless deploy --stage test will keep self:provider.stage as test, but set NODE_ENV to TEST
    • Using the default dev stage will result in NODE_ENV: DEV

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 07:53:06