Hi,
I am working on a Ubuntu 14 Virtual Machine. If i run my node server.js inside the project folder that contains my .env file it's all ok! BUT if i run for example node /home/myapp/server.js it return this error:
{ [Error: ENOENT: no such file or directory, open '.env'] errno: -2, code: 'ENOENT', syscall: 'open', path: '.env' }
and then my server starts on a random port.
Can anyone help me? I need to run the server on startup so i need to add a .conf file on /etc/init with the node /home/myapp/server.js command.
I think we are just logging out this error, your server should be running fine. If you want to get rid of the log you can pass the option of silent: true.
require('dotenv').config({silent: true})
Im assuming your setting your port via .env all you would need to do on your server is set that process variable in you upstart script or something like that
export PORT=5000
By default, the .env file is loaded from process.cwd(). When you run node server.js, the current working directory is /home/myapp/ so your file is found. When your upstart script runs or you run node /home/myapp/server.js, the current working directory is whatever pwd returns so your file isn't found.
Here are two ways to work around this:
require('dotenv').config({path: __dirname + '/.env'});
Use this and you can run node /home/myapp/server.js
__dirname won't work if your app isn't run directly with node, i.e. you're using systemctl. I had to use process.env.HOME instead.
I am getting this error when i tried to install cordova & ionic "sudo npm install -g cordova ionic", what should i do??

I have the file env in directory root. I get this message:
Error: ENOENT: no such file or directory, open '.env'
The value process.cwd() is correct.
Development environment: Ubuntu, Node 6.11.2, NPM 3.10.10
If someone stumbles in this as I did, it's not dotenv fault, somehow if you create the file via a text editor (vscode in my case) node fs module can't read it, I couldn't even do cat .env
the solution is to use the terminal, run touch .env in your project root folder, a new .env file will be created and node fs could read it.
happy coding!
The first .env was created with touch .env command, and you could see that it's using the proper icon.
The second one was created from vscode.
I don't know what's the difference, but apparently not all .env are a .env 馃檲

If you get this error only on Travis CI builds then this my help:
const dotenv = require('dotenv').config()
...
if (process.env.NODE_ENV !== 'test' && dotenv.error) throw dotenv.error
Most helpful comment
require('dotenv').config({path: __dirname + '/.env'});Use this and you can run
node /home/myapp/server.js