Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not get loaded #1

Open
koraysels opened this issue Mar 15, 2016 · 6 comments
Open

does not get loaded #1

koraysels opened this issue Mar 15, 2016 · 6 comments

Comments

@koraysels
Copy link

hi I installed sails-hoos-dotenv and added config in /config but it does not load my .env file..

  active: true
};

I use it for my db credentials etc.. is it possible the hook gets fired too late ? It should be loaded as early as possible right ?

@Taiters
Copy link
Owner

Taiters commented Mar 15, 2016

Hey @koraysels, the hook should run during the sails initialization which should be early enough yeah.

Do you have an example of when you're trying to access the loaded values? (from a model / controller method etc).

Cheers

@jpdavy
Copy link

jpdavy commented Dec 18, 2016

I am having this same issue.

From package.json:

"dependencies": {
  ...
  "sails-hook-dotenv": "^0.1.1",
}

From config/dotenv.js:
module.exports.active = true;

From ./.env:
MONGODB_URI=mongodb://heroku_XXXX:YYYY@ds051334.mlab.com:51334/heroku_ZZZZ

From config/connections.js:
console.log("Database: %s", process.env.MONGODB_URI);

Prints:

sails lift

info: Starting app...

Database: undefined

@jpdavy
Copy link

jpdavy commented Dec 18, 2016

OK I see what is happening here. Sails Lifecycle confirms that config variables are loaded before hooks, so any database variables set in config/connections.js will be loaded before this hook is called.

A suggested quick-fix for anyone coming behind me: just add require('dotenv').config(); to the top of your config/connections.js file.

@0xifis
Copy link

0xifis commented Jul 26, 2017

@jpdavy, I believe it should be require('dotenv').load() to load the dotenv variables.

@jpdavy
Copy link

jpdavy commented Jul 26, 2017

@vishthemenon .load() is an alias of .config(). Either should work fine in this context.

@Virtuallified
Copy link

OK I see what is happening here. Sails Lifecycle confirms that config variables are loaded before hooks, so any database variables set in config/connections.js will be loaded before this hook is called.

A suggested quick-fix for anyone coming behind me: just add require('dotenv').config(); to the top of your config/connections.js file.

Where ever you use the process.env.YOURVAR, insert require('dotenv').config(); before module.export = {}; it will fix your issue for not founding the .env variables in sails.js app

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants