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