您可能希望.dotenv
像这样设置环境变量:
NODE_ENV=local
然后要在调试器中使用它,您需要将其添加到您的launch.json
配置中,例如:
"runtimeArgs": [
"--require=dotenv/config"
]
这是在上下文中:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch | local with dotenv config",
"program": "${workspaceFolder}/bin/www/your_script.js",
"runtimeArgs": [
"--require=dotenv/config"
]
}
]
}
--require=dotenv/config
相当于require('dotenv').config()
在您的脚本中运行,或者node -r dotenv/config your_script.js
如果您使用的是命令行。
下面是一些可以在配置中放置环境变量的替代示例。
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch | local using env file",
"program": "${workspaceFolder}/bin/www/your_script.js",
"envFile": "${workspaceFolder}/.env"
},
{
"type": "node",
"request": "launch",
"name": "Launch | local without dotenv",
"program": "${workspaceFolder}/bin/www/your_script.js",
"env" : {
"NODE_ENV" : "local"
}
}
]
}
注意:此代码尚未经过测试...欢迎提供反馈。