在next.js中设置环境变量

在next.js中设置环境变量,next.js,Next.js,我正在尝试在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 thi

我正在尝试在
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;
  }
});
现在我用这个命令部署我的项目

sls deploy --stage dev | sls deploy --stage prod
如何动态设置
DEPLOY\u STAGE

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

您还可以为命令添加前缀,例如

STAGE=“dev”sls部署
const tsconfig路径splugin=require(“tsconfig路径网页包插件”);
const WITHASS=require(“@zeit/next sass”);
module.exports=withass({
目标:“无服务器”,
环境:{
部署阶段:process.env.STAGE
},
网页包:(配置,选项)=>{
if(config.resolve.plugins){
config.resolve.plugins.push(新的TsconfigPathsPlugin());
}否则{
config.resolve.plugins=[new tsconfig.pathsplugin()];
}
返回配置;
}
});

谢谢您的评论,但效果不如我预期的好。所有部署的Next.js项目都有
节点_ENV
,即
生产