kyun
kyun

Reputation: 10294

set environment variables in next.js

I am trying to set the ENV in Next.js

next.config.js

const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const withSass = require('@zeit/next-sass');
module.exports = withSass({
  target: 'serverless',
  env: {
    DEPLOY_STAGE: stage, // How can I set this dynamically when I deploy it at cli
  },
  webpack: (config, options) => {
    if (config.resolve.plugins) {
      config.resolve.plugins.push(new TsconfigPathsPlugin());
    } else {
      config.resolve.plugins = [new TsconfigPathsPlugin()];
    }

    return config;
  }
});

Now I am deploying my project with this cmd

sls deploy --stage dev | sls deploy --stage prod

How can I set the DEPLOY_STAGE dynamically?

Upvotes: 0

Views: 1288

Answers (2)

Paul van Dyk
Paul van Dyk

Reputation: 1016

You could also prefix your commands, eg;

STAGE="dev" sls deploy
const TsconfigPathsPlugin = require("tsconfig-paths-webpack-plugin");
const withSass = require('@zeit/next-sass');
module.exports = withSass({
  target: 'serverless',
  env: {
    DEPLOY_STAGE: process.env.STAGE
  },
  webpack: (config, options) => {
    if (config.resolve.plugins) {
      config.resolve.plugins.push(new TsconfigPathsPlugin());
    } else {
      config.resolve.plugins = [new TsconfigPathsPlugin()];
    }

    return config;
  }
});

Upvotes: 1

iamhuynq
iamhuynq

Reputation: 5539

try this

env: {
    DEPLOY_STAGE: process.env.NODE_ENV,
},

Upvotes: 0

Related Questions