Environment
Ubuntu
“@openzeppelin /contracts”: “^3.3.0”,
“solc”: “^0.7.0”,
Details
Whenever I try to compile my contract with imported openzeppelin contracts I get an error:
ParserError: Source \"@openzeppelin/contracts/token/ERC20/ERC20.sol\" not found: File import callback not supported\nimport \"@openzeppelin/contracts/token/ERC20/ERC20.sol\
Code to reproduce
pragma solidity ^0.7.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
contract SMPTokenSale {
1 Like
Hi @Alien_Lobster ,
What tool are you using to compile your project?
If you are using Juan Blanco’s Solidity plugin, then the default behavior should pickup OpenZeppelin Contracts in node_modules
, see: https://github.com/juanfranblanco/vscode-solidity#openzeppelin-default
I am using Juan Blanco’s Solidity plugin. I think I’ve resolved this by defining the path to the imports in my compile.js:
const path = require("path");
const fs = require("fs-extra");
const solc = require("solc");
const resolvePath = require("./path-resolve");
const buildPath = path.resolve(__dirname, "build");
fs.removeSync(buildPath);
// All files imported in the contract
const paths = resolvePath({
"ERC20.sol": "../node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol",
"math/SafeMath.sol":
"../node_modules/@openzeppelin/contracts/math/SafeMath.sol",
"GSN/Context.sol": "../node_modules/@openzeppelin/contracts/GSN/Context.sol",
"IERC20.sol":
"../node_modules/@openzeppelin/contracts/token/ERC20/IERC20.sol",
});
const tokenSalePath = path.resolve(__dirname, "contracts", "SMPTokenSale.sol");
const smpTokenSale = fs.readFileSync(tokenSalePath, "utf-8");
const input = {
language: "Solidity",
sources: {
"SMPTokenSale.sol": {
content: smpTokenSale,
},
},
settings: {
outputSelection: {
"*": {
"*": ["*"],
},
},
},
};
function findImports(path) {
if (paths[path]) return { contents: paths[path] };
else return { error: "File not found" };
}
const output = JSON.parse(
solc.compile(JSON.stringify(input), { import: findImports })
).contracts;
fs.ensureDirSync(buildPath);
for (let contract in output) {
fs.outputJSONSync(
path.resolve(buildPath, contract.replace(":", "") + ".json"),
output[contract]
);
}
This seemed to have resolved the errors.
2 Likes
Sean_A
October 24, 2022, 1:33am
4
You cured a 2-3 hour headache for me, thanks! I was able to get errors to go away initially by installing an older version of the plugin but I lost out on some syntax error warnings from the newer versions; now I have everything
1 Like