Compiling OpenZeppelin Contracts in VSCode using solc

I’m trying to create compile a contract on vscode using some openzeppelin contracts I installed through npm and it keeps throwing a parser error

:computer: Environment
Ubuntu
pragma solidity ^0.4.0;
@openzeppelin/contracts”: “^3.3.0”
“solc”: “^0.4.25”,

:memo:Details
Whenever I try to compile I get this error:

[ ':3:1: ParserError: Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File not supplied initially.\nimport "@openzeppelin/contracts/token/ERC20/ERC20.sol";\n^-----------------------------------------------------^\n',
     ':4:1: ParserError: Source "@openzeppelin/contracts/math/SafeMath.sol" not found: File not supplied initially.\nimport "@openzeppelin/contracts/math/SafeMath.sol";\n^-------------------------------------------------^\n' ]
1 Like

I am not familiar with vscode, but this looks like the solc compiler is not configured to look in the right place. If you imported OpenZeppelin contracts using npm, you should make sure that solc is called with the necessary flag to resolve files under node_modules

Additionnaly, I’d like to point out that @openzeppelin/contracts requiers solc ^0.6.0. Version 0.4.x of solidity has been deprecated for a long time, and suffers from many bugs. I very strongly encourage you to move to solidity 0.6 of above:

  • 0.6.x is supported by @openzeppelin/contracts”: “^3.3.0
  • 0.7.x is supported by @openzeppelin/contracts”: “^3.3.0-solc-0.7
  • 0.8.x support is comming very soon !
1 Like

Thanks. Updating my modules fixed the parser error issue.

But now I get this error:

AssertionError [ERR_ASSERTION]: Invalid callback object specified

From what I’ve read the way you compile a contract with new versions of solc has changed.

Here’s my old compile code:
const path = require(‘path’);
const fs = require(‘fs-extra’);
const solc = require(‘solc’);

const buildPath = path.resolve(__dirname, 'build');

fs.removeSync(buildPath);

const tokenSalePath = path.resolve(__dirname, 'contracts', 'SMPTokenSale.sol');
const source = fs.readFileSync(tokenSalePath, 'utf-8');

const output = solc.compile(source, 1);

console.log(output);

fs.ensureDirSync(buildPath);

for( let contract in output){
    fs.outputJSONSync(
        path.resolve(buildPath, contract.replace(':', '') + '.json'),

        output[contract]
    );
}

How do I format this for the new version, if that’s the cause of the Assertion error?

1 Like

Hi @Alien_Lobster,

You may want to look at using either Hardhat or Truffle to develop, test and deploy your smart contracts.
See the OpenZeppelin Learn guides:
https://docs.openzeppelin.com/learn/developing-smart-contracts#setting-up-a-solidity-project