[Solved] Use the truss console to connect to the public blockchain network error: mnemonic invalid or undefined

Error prompt

Error: Mnemonic invalid or undefined

Cause:. Env file naming error

Change the process.env file name directly to .Env and then execute the truss console --network Kovan command

Steps for connecting the common chain of truss
Install dotenv and truffle-hdwallet-provider

npm install dotenv --save-dev -g
npm install truffle-hdwallet-provider --save-dev -g

Note: The windows system may need to restart these two dependent packages to take effect in the project

Create an .env file in the project of root directory, write Mnemonic and INFURA_API_KEY via Form of key-value pairs in the .env file

INFURA_API_KEY= your infura_api_key
MNEMONIC="your mnemonic"

Configure in truffle-config.jsor or truffle.js file:

// Import the dotenv library created to read the settings in the `.env` file
require('dotenv').config();
// import the truffle-hdwallet-provider library to rebuild the wallet
const HDWalletProvider = require('truffle-hdwallet-provider');

module.exports = {
  networks: {
    development: {
     host: "127.0.0.1",     // Localhost (default: none)
     port: 7545,            // Standard Ethereum port (default: none)
     network_id: "*",       // Any network (default: none)
    },
    // Useful for deploying to a public network.
    // NB: It's important to wrap the provider as a function.
    kovan: {
      provider: () => new HDWalletProvider(
        process.env.MNEMONIC, 
        process.env.INFURA_API_KEY
      ),
      gas: 5000000,
      gasPrice: 25000000000,
      network_id: 42
    },
  },
  solc: {
    optimizer: {
      enabled: true,
      runs: 200
    }
  }
}

Enter truffle console --network kovan in the console to connect to the public chain, and you can enter web3.eth.getBlock('latest').then(console.log) for verification. If the following content is returned, the connection is successful:

{ author: '0x03801efb0efe2a25ede5dd3a003ae880c0292e4d',
  difficulty: '340282366920938463463374607431768211454',
  extraData:
   '0xde830206028f5061726974792d457468657265756d86312e33362e30826c69',
  gasLimit: '0x7a1200',
  gasUsed: '0x17d23',
  hash:
   '0xc7390c4f492c8c1da60608135fc9e05930123b645b39f221cba33d8b3c577b2a',
  logsBloom:
   '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000800000000000000000000000000080000000000000000000100000008000000000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400800000000000010000000000000080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009000000008000000',
  receiptsRoot:
   '0x3d05bb2ed4fcc90234eea6d840e7d0e3ce7f598a15e5314536b17bcd11c78b5b',
  sealFields:
   [ '0x84175e8801',
     '0xb84155a8cdb108dccec1d314124058fa6f22e7400ee200db0a94b7b165e4c3454c1818cc05f815cb7ce48f7a88b8401515740311a3566d9cf079428d506a6daca50101' ],
  sha3Uncles:
   '0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347',
  signature:
   '55a8cdb108dccec1d314124058fa6f22e7400ee200db0a94b7b165e4c3454c1818cc05f815cb7ce48f7a88b8401515740311a3566d9cf079428d506a6daca50101',
  size: 877,
  stateRoot:
   '0x03af5adce52a81ce5d332cddb9955e344214bff00859b78868116e1e839efdf7',
  step: '392071169',
  timestamp: 1568284676,
  totalDifficulty: '4524524338444961608702071789512829094373049115',
  transactions:
   [ '0xded7fed0842fd65ec808bc3652ec4175bc190acc11345c49c44b1fb5d954610f',
     '0x7e9112a46fa3c07aad813ea86355b15eebb44023c040d198ee7d15d379bbc2be' ],
  transactionsRoot:
   '0x0dd10d90686dda2684bd0ba70d1c9e1d9a5302c30ca75eb2c5b07a7b6e4498b9',
  uncles: [] }

Note: every time you want to call the contents of the. Env file, you must first import the dotenv Library in the JS file, otherwise you cannot read the contents of the file

Read More: