我有一个应用程序,在本地(不使用pm2)中,.env
文件中的所有环境变量都可以很好地使用 dotenv
。
但是在我使用 pm2
运行该应用程序的服务器上,环境变量仍然为 undefined
。
我在服务器上使用的 pm2 命令是:
pm2 start myapp/app.js
pm2 startup
pm2 save
我有一个应用程序,在本地(不使用pm2)中,.env
文件中的所有环境变量都可以很好地使用 dotenv
。
但是在我使用 pm2
运行该应用程序的服务器上,环境变量仍然为 undefined
。
我在服务器上使用的 pm2 命令是:
pm2 start myapp/app.js
pm2 startup
pm2 save
dotenv
会读取当前目录中的.env
文件。
当您调用pm2 start myapp/app.js
时,它不会搜索myapp/.env
文件。
.env // It will try to load this, which doesn't exist
myapp/
app.js
所以你有两个解决方案
使用 path
选项:
const path = require('path');
require('dotenv').config({ path: path.join(__dirname, '.env') });
myapp/
内部调用您的脚本。pm2 start app.js
一种好的做法是从代码中删除dotenv,并在命令行上“require”它。这使得您的代码在任何环境(包括基于云的环境)之间很方便地移植 - 这是环境变量的主要特点之一。
注意:在服务器上运行时,仍需要通过npm在项目中安装dotenv。
a)将.env文件与脚本(例如app.js)放在一起编写代码
b)如果要在没有pm2的情况下运行脚本:
node -r dotenv/config app.js
c) 在 pm2.config.js 文件中:
module.exports = {
apps : [{
name : 'My Application',
script : 'app.js',
node_args : '-r dotenv/config',
...
}],
}
然后执行pm2 start pm2.config.js
注意:在命令行上使用dotenv/config是dotenv本身推荐的最佳实践之一
2021年更新:为了完整起见,由于我的答案得到了一些赞,我想将第4个选项添加到列表中:
d) 合并pm2/env配置
module.exports = { apps : [{
name : 'My Application',
script : 'app.js',
env : {
PORT: 5010,
DB_STRING: 'mongodb://localhost:27017',
...
},
}]};
如果你将你的pm2.config视为环境配置并且不使用git等工具,这将非常有用。它消除了单独使用.env文件的需求,这可能适合你。它完全取代了dotenv,因为pm2会将env变量注入到你的脚本进程中。
node_args: '-r dotenv/config'
,则会出现错误。 - sarfarazsajjad你需要先停止你的PM2进程
尝试
pm2 kill
然后使用以下命令重新启动pm2
pm2 start app.js
我曾经遇到过同样的问题,但是解释不够清楚,于是根据GitHub用户vmarchaud的评论得出了解决方案。 这个方法也可以修复人们在@Andy Lorenz解决方案中遇到的问题。
在我的情况下,我想要为多个应用程序创建一个生态系统文件,但是我一直遇到以下问题:
Error: Cannot find module 'dotenv/config'
解决方案很简单。你需要声明cwd,也就是dotenv/config将要读取的项目文件夹。
module.exports = {
apps: [{
name: 'app1 name',
script: 'app1.js',
cwd: '/path/to/folder/',
exec_mode: 'fork_mode',
node_args: '-r dotenv/config',
}, {
name: 'app2 name',
script: 'app2.js',
cwd: '/path/to/folder/',
instances: 'max',
exec_mode: 'cluster',
node_args: '-r dotenv/config',
}],
};
dotenv
库解析 .env
文件,并在 ecosystem.config.js
中手动设置它们。
ecosystem.config.js:
const { calcPath, getEnvVariables } = require('./helpers');
module.exports = {
apps: [
{
script: calcPath('../dist/app.js'),
name: 'dev',
env: getEnvVariables(),
},
],
};
helpers.js:
const path = require('path');
const dotenv = require('dotenv');
const fs = require('fs');
function calcPath(relativePath) {
return path.join(__dirname, relativePath);
}
// this function will parce `.env` file but not set them to `process.env`
const getEnvVariables = () => {
const envConfig = dotenv.parse(fs.readFileSync(calcPath('.env')));
const requiredEnvVariables = ['MODE'];
for (envVariable of requiredEnvVariables) {
if (!envConfig[envVariable]) {
throw new Error(`Environment variable "${envVariable}" is not set`);
}
}
return envConfig;
};
对我来说这些都不起作用,因为我正在使用集群模式。
我将dotenv作为开发依赖项安装在根目录下(我也使用了yarn工作区)。
然后我做了这个:
require('dotenv').config({ path: 'path/to/your/.env' })
module.exports = {
apps: [
{
name: 'app',
script: 'server/dist/index.js',
instances: 2,
exec_mode: 'cluster',
instance_var: 'APP_INSTANCE_SEQ',
// listen_timeout: 10000,
// restart_delay: 10000,
}
]
}
这是我的项目设置..
/src/app.ts
然后编译成 dist 文件夹。
/dist/app.js
我的 .env 文件在 dist 文件夹外面,所以它是无法访问的。
这是我尝试的命令。 pm2 start app.js --env=.env
.env
app.js
.env
文件中,并将其传递到app.js
中:
在.env文件内部。token=value
require('dotenv').config();
console.log(process.env.token)
另外,不要忘记。如果您将.env文件添加到.gitignore中,然后在VPS或其他地方上使用git pull
,您需要手动复制.env文件,否则您的应用程序将无法正常工作。
在某些情况下,您使用配置的区域很重要,因此请确保将NODE_ENV=production
字符串添加到您的.env文件中。
最后,您可以直接从应用程序文件夹中使用pm2 start app.js
。
require('dotenv').config({path: `${__dirname}/.env`});
也可以正常工作。 - Chris Broskipath.join
会处理它,并且在任何操作系统中都可以工作。 - Marcos Casagrandepath.join
的原因,因为它是通用答案。 - Marcos Casagrande