ParserError: Source "@openzeppelin/contracts/token/ERC20/IERC20.sol" not found: File import callback not supported

Hello there,

Structure of my folder:

  • package.json
  • node_modules/
  • ethereum/
    • compile.js
    • contracts/
      • TokenSwap.sol

Import in my TokenSwap.sol contract:

pragma solidity ^0.8.11;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

I am trying to compile my contract locally with solc but I get the following error:
ParserError: Source "@openzeppelin/contracts/token/ERC20/IERC20.sol" not found: File import callback not supported

I used npm i @openzeppelin/contracts

The command I use in my package.json file:
"compileContract": "node ethereum/compile.js",

Dependencies:

"dependencies": {
    "@openzeppelin/contracts": "^4.4.2",
    "solc": "^0.8.11",

My compile.js script:

const path = require("path");
const solc = require("solc");
const fs = require("fs-extra");

const tokenswapPath = path.resolve(__dirname, "contracts", "TokenSwap.sol");

const source = fs.readFileSync(tokenswapPath, "utf8");

var input = {
  language: "Solidity",
  sources: {
    "TokenSwap.sol": {
      content: source,
    },
  },
  settings: {
    outputSelection: {
      "*": {
        "*": ["*"],
      },
    },
  },
};

const output = JSON.parse(solc.compile(JSON.stringify(input)));

console.log(output);

No matter how I format the import path for OpenZeppelin I get the same error

Please help :pray:

A little weird, so what is your local env? I think your can delete the node_modules/ and then reinstall to have a try.

I ended up compiling my contract through command line with solcjs

package.json
"compileContract": "solcjs --bin --abi --include-path node_modules/ --base-path . -o ethereum/contracts/build ethereum/contracts/TokenSwap.sol",