Setting up truffle.config file with .env using Moralis Nodes

Can anyone tell me how to properly set the Ropsten network with a .env file. I believe the problematic line here is… provider: () => new HDWalletProvider(mnemonic, `https://speedy-nodes-nyc.moralis.io/${process.env.ROPSTEN_MORALIS_PROJECT_ID}/eth/ropsten`), I don’t believe i am formatting this correcty. Anyone know how to correctly format this and set this up? Thanks

const dotenv = require("dotenv");
dotenv.config();

const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = process.env.ROPSTEN_MNEMONIC;

module.exports = {

  networks: {
    development: {
      host: "127.0.0.1",     // Localhost (default: none)
      port: 8545,            // Standard Ethereum port (default: none)
      network_id: "*",       // Any network (default: none)
    },

    ropsten: {
      provider: () => new HDWalletProvider(mnemonic, `https://speedy-nodes-nyc.moralis.io/${process.env.ROPSTEN_MORALIS_PROJECT_ID}/eth/ropsten`),
      network_id: 3,       // Ropsten's id
      gas: 5500000,        // Ropsten has a lower block limit than mainnet
      confirmations: 2,    // # of confs to wait between deployments. (default: 0)
      timeoutBlocks: 200,  // # of blocks before a deployment times out  (minimum/default: 50)
      skipDryRun: true     // Skip dry run before migrations? (default: false for public nets )
    },
  },

if you hardcode it then it works fine?
can you add a console.log for that string to see what it shows?

No it doesn’t work. I am sure it is formatted wrong, i just don’t know how to format it for Moralis nodes and couln’t find any documentation on it. It works with infura.

if you hardcode the entire url, like: https://speedy-nodes-nyc.moralis.io/31321/eth/ropsten, does it work?

then, if you add the entire url to env, does it work?

what you get if you do that console log? just to make sure you have the expected data there

It worked fine if I kept the entire URL in there. Its only because i replaced it with the process.env stuff that it failed. I’m not sure where to put the console.log but tbh it is completley unncessary, The formatting is not correct with the process.env. stuff. If you have an example to send over that is formatted correctly just let me know. Thanks

What does your .env file look like in your project’s root directory? It should look like:

ROPSTEN_MORALIS_PROJECT_ID=idhere

And just do require("dotenv").config at the top of your Truffle config.

You can log process.env.ROPSTEN_MORALIS_PROJECT_ID directly to make sure it’s valid.

Thanks for the reply.
My .env looks exactly like you showed.
So you are saying this line…

provider: () => new HDWalletProvider(mnemonic, https://speedy-nodes-nyc.moralis.io/${process.env.ROPSTEN_MORALIS_PROJECT_ID}/eth/ropsten)

Can be adjusted to the following?….

provider: () => new HDWalletProvider(mnemonic, process.env.ROPSTEN_MORALIS_PROJECT_ID )

No you still need the full URL of the node as in your original example. Remember that things worked once you hardcoded it.

Did you change your require to require("dotenv").config()?

Dot env is fine and not the issue. I guess we are miscommunicating here of perhaps I am not explaining correctly. I am tryin to set up my truffle.config file up to avoid putting in my project ID. I would prefer to leave out those details as that is the point of the .env file. So the issue is formatting this line correctly so the project ID is replaced with the process.env stuff. Makes sense? I’ve set this up a million times with Infura but the fomenting is a bit diff for moralis so I’m not sure how to format it. If you have a project with a truffle.config file that uses process.env in place of the project ID than maybe sending over a link would be best.
Otherwise maybe just type out the provider line exactly the way it’s supposed to be formatted with the mnemonic and process.env. That would be very clear if you typed it out assuming you know how to format it. Thanks.

provider: () => new HDWalletProvider(mnemonic, https://speedy-nodes-nyc.moralis.io/${process.env.ROPSTEN_MORALIS_PROJECT_ID}/eth/ropsten)

Did you try console.log for process.env.ROPSTEN_MORALIS_PROJECT_ID? It must be an issue here as it worked hardcoded and now it isn’t when you try and substitute the id. Using environment variables in terms of syntax in this way is the same whether it’s for Infura or Moralis. You are just putting the id in between https://speedy-nodes-nyc.moralis.io/ and /eth/ropsten.

Are you missing the `` around your node string in your code? I noticed it’s fine in your original example but now you’re posting it without it.

Infura is different because you just put the process.env stuff at the end of the entire URL. Here I am guessing it goes between the first part and the end of it which is /eth/ropsten but i am simply “guessing” it goes in between. I am just guessing that it is substituted where i substituted it but I don’t know for sure, thats why i am asking. If that is not the problem and it turns out I guessed correctly about where to substitute it than perhaps it is another issue but it is not formatted correctly or it would work.

I don’t know where i would place the console.log. Again I think the best thing would be to see an example that is coded correctly. Can you provide that or type out how to code it. Otherwise you are making guesses like me which isn’t working and we are going in circles. All good if you don’t know, maybe someone else will spot this and send over an example that is coded correctly with process.env.

There’s no guesswork involved here, that’s how the URL is formatted. You’re just putting it where the ID is meant to go.

Infura:
https://mainnet.infura.io/v3/idhere
https://mainnet.infura.io/v3/${process.env.INFURA_ID}

Moralis:
https://speedy-nodes-nyc.moralis.io/idhere/eth/mainnet
https://speedy-nodes-nyc.moralis.io/${process.env.MORALIS_ID}/eth/mainnet

Or you can just put the whole URL in your environment variable if this is confusing; I suggest you just do this and not worry about the substitution.

// .env
ROPSTEN_URL=https://speedy-nodes-nyc.moralis.io/idhere/eth/ropsten

....
// truffle-config.js
require('dotenv').config()
const HDWalletProvider = require('@truffle-hdwallet-provider')
const MNEMONIC = process.env.MNEMONIC
const ROPSTEN_URL = process.env.ROPSTEN_URL

module.exports = {
    networks: {
        ropsten: {
            provider: () => new HDWalletProvider(MNEMONIC, ROPSTEN_URL),
            network_id: 3
        },
    }
};

Okay I am surprised it didn’t work if it was coded correctly, but i will go ahead and give it try without the substitution and defining it at the top like you showed. Thank you