Getting MongoParseError: URI malformed when trying to run movie-last-updated-migration.js in Migrations Ticket

In the migrations ticket, I added my host as:

const host = process.env.MFLIX_DB_URI;

In my .env file, I have:

MFLIX_DB_URI=mongodb+srv://m220student .... mongodb.net/test?retryWrites=true

I have also tried it without the /test?retryWrites=true at the end

and when I node movie-last-updated-migration.js in the migrations folder, I get error:

Error during migration, MongoParseError: URI malformed, cannot be parsed

What’s wrong?

When I type the URI directly onto the js file, it works. Not sure why it doesn’t work with process.env…

Hi @Christy_07873

It looks like a NodeJS parse error from the error message. It should normally be a case of cutting and pasting the output from your Atlas UI to the .env file for MFLIX_DB_URI.

Could you also try MFLIX_DB_URI=mongodb+srv://m220student .... mongodb.net/test without the ?retryWrites=true at the end ?

Hope this helps!
Eoin

Yes, it does work when I copy and paste the URI directly, just wondering why Node can’t parse it if it’s in .env

Did try without the last part on the URI

You could try to percent-encode the ‘@’ replacing it with %40 but I believe the NodeJS parser (at least recent versions of the library) should not need to do this.

What version of NodeJS are you running ? Can you double check it’s a recent version ?

Hope this helps!
Eoin

Hello @Christy_07873 -

Was hitting same issue.

Finally realized - with this migration script we’re simply running node directly on this single script - no prior setup, no “index.js” to attend to things like environment configuration and so forth, the way we have it when we use npm start to kick off the whole running of the app + api.

So, I finally brought one line from the top-most /mflix-js/index.js into the simple movie-last-updated-migration.js script, namely:

  • require("dotenv").config()

and that got it to work.
Without that, “process.env” I think fails to do anything, find anything.

I think you were on the right track, to try to keep your database password out of any source code! (even if it was a simple data processing script, sort of on the side, outside scope of the rest of the running app + api). Still, not something you’d want to get into a Git repository…

Best,
William R.

1 Like

Thanks for explaining a little of what’s going on. I didn’t have that line in index.js and putting it into the migration script didn’t change anything. I may be missing some things. If you have a minute, can you paste the first few lines of those files?

UPDATE: I found the correct index.js file and saw that I had the correct require and export statements as @Kanika listed. (I was just looking at a different index.js). Still cannot link to the .env file; adding require("dotenv").config() in migration script doesn’t seem to solve it.

Top of migrations script looks like:

require("dotenv").config();
const MongoClient = require("mongodb").MongoClient;
const ObjectId = require("mongodb").ObjectId;
const MongoError = require("mongodb").MongoError;```

Hi @Christy_07873,

Please find the contents of mflix-js/index.js file here:

require("dotenv").config()

exports = module.exports = require("./src")  

If that does not help, please share the full connection string in your migration ticket.

Kanika

I’ve been stuck a for a while on this issue as well, and what finally worked for me was to include the dotenv with the relative path in the movie-last-updated-migration.js

require("dotenv").config({ path: '../../.env' })
15 Likes