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

when using dotenv error - URL malformed, cannot be parsed #9

Closed
mathiesha opened this issue Apr 10, 2018 · 6 comments
Closed

when using dotenv error - URL malformed, cannot be parsed #9

mathiesha opened this issue Apr 10, 2018 · 6 comments

Comments

@mathiesha
Copy link

mathiesha commented Apr 10, 2018

following the guide on the official nestjs docs, when trying to load a db connection string via a dotenv file, a error is thrown "URL malformed, cannot be parsed".

e.g.
dotenv file

db_connection = 'mongodb://localhost:27017/foo'

root.module

@ module({
modules: [
MongooseModule.forRoot(process.env.db_connection),
....
],
})

not sure if this is an implementation error from my part.

thanks,

@nixoncode
Copy link

the problem is not with mongoose or dotenv it has to do with the way you are initializing dotEnv package.
Please ensure that on the file where you are creating the mongoose connection you are also requiring dotEnv.

  require('dotenv').config()

after that line the you can now initialized mongoose

  mongoose.connect(process.env.MONGO_URI)
    .then(connection => {
        console.log('Connected to MongoDB')
    })
    .catch(error => {
      console.log(error.message)
     })

hope that helps.

@mathiesha
Copy link
Author

mathiesha commented Apr 16, 2018

@nickhq thanks a lot, was a stupid mistake I was doing. I can confirm your suggestion works.

@nixoncode
Copy link

Awesome! You can go ahead and close it now

@artworkad
Copy link

@mtziyo the issue is describe here in more detail https://github.com/motdotla/dotenv#how-do-i-use-dotenv-with-import

I like the approach of removing dotenv.config from my project and adding following script to package.json

"start:local": "ts-node --require dotenv/config -r tsconfig-paths/register src/server.ts"

Because in staging/production environments I don't need dotenv at all.

@nixoncode
Copy link

nixoncode commented Jul 5, 2018 via email

@mathiesha
Copy link
Author

hey @artjomzab, this is a nice approach thanks for sharing :)

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

3 participants