Source file requires different compiler version : Truffle
Asked Answered
D

5

6

I have written one simple smart contract in solidity and trying to migrate it with truffle.

$ truffle migrate
Compiling .\contracts\Election.sol...
Compiling .\contracts\Migrations.sol...

    /D/ethereum/electiondemo/contracts/Migrations.sol:1:1: SyntaxError: Source file requires different compiler version (current compiler is 0.5.0+commit.1d4f565a.Emscripten.clang - note that nightly builds are considered to be strictly less than the released version
    pragma solidity ^0.4.24;
    ^----------------------^

Compilation failed. See above.`enter code here`
Truffle v5.0.0 (core: 5.0.0)
Node v8.11.1

Solidity version is 0.5.0. Please find below the code for the smart contract:

pragma solidity ^0.5.0;

contract Election {
    // Read/write candidate
    string public candidate;

    // Constructor
    constructor ( ) public {
        candidate = "Candidate 1";
    }
}
Duck answered 30/12, 2018 at 7:58 Comment(0)
D
17

Got the solution : In truffle.js. You need specify solidity version

module.exports = {
   // See <http://truffleframework.com/docs/advanced/configuration>
   // for more about customizing your Truffle configuration!
   networks: {
       development: {
           host: "127.0.0.1",
           port: 7545,
           network_id: "*" // Match any network id
       }
   },
   compilers: {
       solc: {
           **version: "0.4.24"** // ex:  "0.4.20". (Default: Truffle's installed solc)
       }
   }
};

Same need to given in your smart contract

Duck answered 30/12, 2018 at 8:5 Comment(0)
B
11

Add the below line to truffle-config.js

{
  compilers: {
    solc: {
      version: "0.4.24" // ex:  "0.4.20". (Default: Truffle's installed solc)
    }
  }
}
Brandenburg answered 8/1, 2019 at 20:54 Comment(0)
M
3

As of now, truffle uses '0.5.16' as default. So if your code is using newer solidity versions, it would throw an error. you do not need to put a specific value for the solc version.

this is what I use on the contract

 pragma solidity >=0.7.0 <0.9.0;

in the config file

compilers: {
    solc: {
      // default is 0.5.16
      version: ">=0.7.0 <0.9.0",    // Fetch exact version from solc-bin (default: truffle's version)
    
      }
    }
  },
Maturity answered 10/7, 2021 at 20:56 Comment(0)
I
0

Your migration contract (Migrations.sol) needs 0.4.24.

Go to your migration contract and change your dependency to 0.5 or change your main contracts dependency to 0.4.*

Insulate answered 7/1, 2019 at 17:4 Comment(0)
A
0

to your truffle.js / truffle-config.js add this

module.exports = {
// See <http://truffleframework.com/docs/advanced/configuration>
// for more about customizing your Truffle configuration!
networks: {
  development: {
    host: "127.0.0.1",
    port: 7545,
    network_id: "*" // Match any network id
  }
},
compilers: {
  solc: {
    version: "0.4.24" //(Default: Truffle's installed    solc)
  }
 }
};

Then use npx to run your packages. npx is a native npm package, so it comes with your installation of nodejs & npm. It allows you to run local node package binaries. This way, you can get rid of a lot of your global package installations, and use the local binaries that are defined in your package.json.

npx truffle compile

npx truffle test (optional)

npx truffle migrate

Arvie answered 4/12, 2020 at 10:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.