[Ethereum] “Migrations” hit an invalid opcode while deploying on deploy Migrations.sol with Truffle

migrationsoliditytruffletruffle-compiletruffle-migration

An error occurs when truffle migrate try to deploy Migrations.sol contract, i don't understand why

pragma solidity >=0.4.21 <0.6.0;

contract Migrations {
  address public owner;
  uint public last_completed_migration;

  constructor() public {
    owner = msg.sender;
  }

  modifier restricted() {
    if (msg.sender == owner) _;
  }

  function setCompleted(uint completed) public restricted {
    last_completed_migration = completed;
  }

  function upgrade(address new_address) public restricted {
    Migrations upgraded = Migrations(new_address);
    upgraded.setCompleted(last_completed_migration);
  }
}

Migrations.sol

1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
Error: Error: Error:  *** Deployment Failed ***

"Migrations" hit an invalid opcode while deploying. Try:
   * Verifying that your constructor params satisfy all assert conditions.
   * Verifying your constructor code doesn't access an array out of bounds.
   * Adding reason strings to your assert statements.

    at Object.run (C:\Users\Sim'S\AppData\Roaming\npm\node_modules\truffle\build\webpack:\packages\truffle-migrate\index.js:92:1)
    at process._tickCallback (internal/process/next_tick.js:68:7)
Truffle v5.0.21 (core: 5.0.21)
Node v10.15.3

Error message

Best Answer

Just ran into the same error. I was running an older version of ganache-cli in this case and had to update ganache, so what helped was running

npm install -g ganache-cli

in my case.