1

I get 'File import callback not supported' when I try to compile my simple solidity contract, that imports ERC721. Can anybody help?

Here is what I have already done:

I have installed openzeppelin in my project folder:

install openzeppelin

This is the solidity contract:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract Mycoin is ERC721 {
    constructor() ERC721("MyCoin", "MYCO") {} 
}

This is my compile script:

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

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

const coinPath = path.resolve(__dirname, "contracts", "mycoin.sol");
const src = fs.readFileSync(coinPath, 'utf8');

const input = {
  language: "Solidity",
  sources: {
    "mycoin.sol": {
      content: src,
    },
  },
  settings: {
    outputSelection: {
      "*": {
        "*": ["*"],
      },
    },
  },
};

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

fs.ensureDirSync(buildPath);

for (let contract in output.contracts["mycoin.sol"]) {
  fs.outputJSONSync(
    path.resolve(buildPath, contract + ".json"),
    output.contracts["mycoin.sol"][contract]
  );
} 

I'm using VSC, and the editor indicates that it can see the openzeppelin file. But apparently the compiler can not find the openzeppelin file. Because I get an error (file import callback not supported) when I run the compile script:

file import callback not supported

I would like to make this project without Truffle/Hardhat, if it is possible.

2

1 Answer 1

1

Take a Look at @Julissa DC answer.

Alternate Solution -

Instead of importing Openzeppelin from package. You can directly paste code from github and import it.

Your code will look like

pragma solidity ^0.8.0;

import "./ERC721.sol";
import "./IERC721Receiver.sol";
import "./IERC721Metadata.sol";
import "./Address.sol";
import "./Context.sol";
import "./Strings.sol";
import "./ERC165.sol";


contract Mycoin is ERC721 {
    constructor() ERC721("MyCoin", "MYCO") {} 
}

And Your Root Directory should look like

├── Mycoin.sol
├── ERC721.sol
├── IERC721Receiver.sol
├── IERC721Metadata.sol
├── Address.sol
├── Context.sol
├── Strings.sol
├── ERC165.sol
├── IERC165.sol

Include all above files in you project in root directory, or in any directory you are confortable with. Like wise edit import path of files.

NOTE - Change import statements in Files according to path
i.e. import "./extensions/IERC721Metadata.sol"; to import "./IERC721Metadata.sol";

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.