您可能希望将 .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"
}
}
]
}
注意:此代码尚未经过测试...欢迎提供反馈。