Dotenv: fs.readFileSync is not a function error

Created on 8 Oct 2016  路  8Comments  路  Source: motdotla/dotenv

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.

Most helpful comment

@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.

All 8 comments

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!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

AndersDJohnson picture AndersDJohnson  路  4Comments

AndrewChen1982 picture AndrewChen1982  路  4Comments

acharotariya picture acharotariya  路  3Comments

shellscape picture shellscape  路  3Comments

danawoodman picture danawoodman  路  3Comments