I followed the installation procedure, I get the following error by just requiring dotenv:
fs.readFileSync is not a function
I'm using node v6.6.0.
Would you mind posting your .env file?
readFileSync is a function that's been around since v0.1.8 of Node.js. Really unlikely this is a problem with dotenv
@maxbeatty In dotenv/lib/main.js on line 30, you have the following snippet:
try {
// specifying an encoding returns a string instead of a buffer
var parsedObj = this.parse(fs.readFileSync(path, { encoding: encoding }))
...
That also happens to be the snippet the console error is referring to.
@Bobeta - You might also check into whether this is a node v6.x.x issue by trying an older version of node.
@Bobeta there seems to be something with your configuration. Just tested
node v6.6.0 (npm v3.10.3)
node -r dotenv/config -e 'console.log(process.env.FOO)'
and
node -e "require('dotenv').config(); console.log(process.env.FOO)"
both worked as expected.
Thanks folks for your replies!
@jcblw It works for me as well in node console but not in the project. Might be worth to mention that I use watchify and babelify.
I just figured that it's probably a server side library, didn't look deep enough into it earlier as I just needed a quick solution to store my config files for some test project. I'll look into using some server side alternatives.
@Bobeta dotenv is a server side library, and will not work if your include it into your browser bundle. I would suggest using something like envify with dotenv to achieve this.
@jcblw I figured that out as I was typing the comment, wouldn't make sense to have it in the browser bundle. Silly me.. Thanks for your help!
Most helpful comment
@Bobeta
dotenvis a server side library, and will not work if your include it into your browser bundle. I would suggest using something like envify withdotenvto achieve this.