0

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?

2 Answers 2

1

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;
  }
});
Sign up to request clarification or add additional context in comments.

Comments

0

try this

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

1 Comment

Thank you for your comment, but it doesn't work as much as I expected. All deployed Next.js projects have NODE_ENV that is production.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.