nodejs打字稿编译器在构建时传递环境文件

问题描述 投票:0回答:1

我正在使用typescript开发一个简单的nodejs应用程序。当我要在不同的环境中部署此应用程序时,我需要在构建应用程序时告知要使用哪个环境文件。

npm run build (node_modules/.bin/tsc)

[在构建应用程序时,有没有办法告诉tsc他应该使用哪个env文件?以及如何在代码中实现呢?

node.js typescript tsc
1个回答
0
投票

我不确定您是否可以通过TSC做到这一点。

但是您可以创建一个单独的JSON文件,其中将包含您的env特定项目。构建您的应用程序后,它将包含来自JSON的正确设置。

考虑使用此环境变量

if(process.env.DEPLOYMENT == "prod"){
  fs.writeFile("config.json", JSON.stringify({
  // your specific production settings come here
  loginEndpoint: "https://productionDomain.com/login"
}))

if(process.env.DEPLOYMENT == "local"){
 fs.writeFile("config.json", JSON.stringify({
 // your specific local configuration and so on
 loginEndpoint: "http://localhost:3000/login"
}))
© www.soinside.com 2019 - 2024. All rights reserved.