[英]nodemon, babel-node: how to set environment variable?
Dev environment starts with this command:开发环境以这个命令开始:
nodemon -w src --exec \"babel-node src --presets es2015,stage-0\"
How do i create a global variable (or process.env
variable) __DEV__ = true
?如何创建全局变量(或
process.env
变量) __DEV__ = true
?
You can either add "env"
property to nodemon.json
, like this:您可以将
"env"
属性添加到nodemon.json
,如下所示:
...
"env": {
"__DEV__": "true"
}
Or you can prepend __DEV__="true"
to start
script in package.json
.或者您可以在
package.json
__DEV__="true"
来start
脚本。 Both worked for me.两者都为我工作。
You can add a "nodemonConfig" property to package.json with your env info.您可以使用您的 env 信息将“nodemonConfig”属性添加到 package.json。 Then execute nodemon in your scripts section.
然后在脚本部分执行 nodemon。
"nodemonConfig": {
"restartable": "rs",
"ignore": [
"node_modules/**/node_modules"
],
"delay": "2500",
"env": {
"NODE_ENV": "development",
"NODE_CONFIG_DIR": "./config"
}
}
If you don't want to handle the env variables in the nodemon call, you can do something like this.如果你不想在 nodemon 调用中处理 env 变量,你可以做这样的事情。
1) Create a file called '.env' and put something like this in it: DEV =true 1) 创建一个名为 '.env' 的文件并在其中放入如下内容: DEV =true
2) Then in your application entry file put the following line in as early as possible: 2)然后在您的应用程序入口文件中尽早输入以下行:
require('dotenv').config(); require('dotenv').config();
I normally use the dotenv module on my projects.我通常在我的项目中使用dotenv模块。
We just need to create a .env
file and require the dotenv
module in our project:我们只需要创建一个
.env
文件并在我们的项目中dotenv
模块:
.env
file : .env
文件:
__DEV__="true"
your-script.js
file : your-script.js
文件:
require('dotenv').config();
console.log(process.env.__DEV__)
Creating .env
files is normally a good option since we can prevent to commit our environment files using .gitignore
创建
.env
文件通常是一个不错的选择,因为我们可以防止使用.gitignore
提交我们的环境文件
对于 Windows:设置DEV = true&&nodemon -w src --exec "babel-node src --presets es2015,stage-0"
只需在代码(服务器文件)中定义proccess.env.VARIABLE="true"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.