I am trying to compile solidity code in solc(nodejs) but unsuccessful so far. I have created a sample app for demo. Here is sample's directory structure (node.js and truffle combined):

│   app.js
│   compile.js
│   deploy.js
│   routes.js
│   truffle-config.js
│   └───contracts
│           A.json
│           Migrations.json
│           U.json
│       A.sol
│       Migrations.sol
│       U.sol
│       1_initial_migration.js
│       2_deploy.js

Here is the link to source: https://drive.google.com/file/d/1jeBRM_F0PfAqBZy0JWK3gKv53M9mxdcC/view?usp=drivesdk

npm packages:

npm i express path fs solc ethereumjs-tx web3-utils [email protected] morgan

It works in truffle both getTotalCount() and add() i.e.,

truffle(development)> let a = await A.deployed()
truffle(development)> a.getTotalCount()
<BN: 0>
truffle(development)> a.add(12)
{ tx:
   { transactionHash:
     transactionIndex: 0,
     blockNumber: 164,
     from: '0x07df0e5ab4793197007a867d97a3aeeea5347456',
     to: '0x8b421f5c74b48e0f6ae763702754ce172deeb293',
     gasUsed: 47006,
     cumulativeGasUsed: 47006,
     contractAddress: null,
     logs: [],
     status: true,
     v: '0x1c',
     rawLogs: [] },
  logs: [] }
truffle(development)> a.getTotalCount()
<BN: 1>

but when type localhost:34000/d in postman i get:

GET /d 200 4051.218 ms - 12
Error: VM Exception while processing transaction: invalid opcode
    at Object.InvalidResponse (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\web3\lib\web3\errors.js:38:16)
    at D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\web3\lib\web3\requestmanager.js:86:36
    at XMLHttpRequest.request.onreadystatechange (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\web3\lib\web3\httpprovider.js:129:7)
    at XMLHttpRequestEventTarget.dispatchEvent (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (D:\assignments\blockchain\assign\assignments\blockchain\src\anarik-4\anarik-v5.0\src_3\anarik-v5.0.2\test\smartContract\solc\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19) null
    throw new ERR_HTTP_HEADERS_SENT('set');

Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
    at ServerResponse.setHeader (_http_outgoing.js:470:11)

Apps' versions:

> truffle version
Truffle v5.0.2 (core: 5.0.2)
Solidity - 0.5.2 (solc-js)
Node v10.14.2

solc npm package version:

  "_id": "[email protected]",

Ganache: v2.0.0-beta.2

Its been days that I am trying to solve this problem but can't. All methods in my contract give invalid opcode error when compiled via solc, but in truffle it seems to work fine, and I can't identify source of the problem.

  • I'd suggest first try updating ganache. What do calls do you make when /d invoked? What do you do in your contract functions look like? It is better if you reduce your example to the minimal code that causes the error.
    – Ismael
    Apr 20, 2019 at 6:53
  • I am suspicious of the solc version difference but more suspicious of the postman testing methodology. We can see that it works just fine when the request is well-formed with truffle. While I don't dive into RPC every day it just doesn't seem like a complete message with a sender and a function signature - localhost:34000/d. What about the rest of it? Have a look at this for possible hints: ethereum.stackexchange.com/questions/3514/… Apr 20, 2019 at 22:04

2 Answers 2


New opcodes were introduced in Constantinople/St. Petersburg hard fork. Solidity compiler was able to produce such opcodes since 0.4.24, but this feature became enabled by default only in 0.5.5. Ganache Core supports this opcodes since version 2.5.1, but Ganache 2.0.0-beta.2 is based on Ganache Core 2.3.3. That's why you contract failed to work.


After updating Ganache to latest version(v2.0.1) it worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.