31

Tried to run:

1.) Dappuniversity project (dappuniversity/dbank) 2.) pet-shop-tutorial

Truffle v5.3.3 (core: 5.3.3) Node v14.15.5

How can ser compile code @ the 0.8.4 to import OpenZeppelin’s ERC20.sol template, when Truffle requires it’s compiler/solc to match 5.3.3?

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

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

contract Token is ERC20 {

  //add minter variable

  //add minter changed event

  constructor() public payable ERC20("Name", "Symbol") {

    //assign initial minter

  }

  //Add pass minter role function

  function mint(address account, uint256 amount) public {

    //check if msg.sender has a minter role

    _mint(account, amount);

  }

}

Source “@openzeppelin/contracts/token/ERC20/ERC20.sol” not found: File import callback not supported

Yilmaz
  • 35,338
  • 10
  • 157
  • 202
GoGetterMeme
  • 429
  • 1
  • 5
  • 5

19 Answers19

65

The Error:
Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File import callback not supported enter image description here

Step 1:

Install the Solidity Extention enter image description here

Step 2:

  • Right click on the error.
  • Select "Change the default workspace..." enter image description here

Step 3:

Select localNodeModule
Might have to restart the IDE enter image description here

Asolace
  • 1,006
  • 8
  • 9
9

For me (running Win 10) this error resolved when I cleared out a setting in the VSCode solidity extension.

Extensions menu
--> Right click Solidity by Juan Blanco
--> Extension Settings
--> Scroll to "Solidity:Package Default Dependencies Contracts Directory"
--> Delete the default value

The default value was pointing things to the wrong path.

https://github.com/juanfranblanco/vscode-solidity/issues/178

Peter Csala
  • 17,736
  • 16
  • 35
  • 75
APerson1000
  • 91
  • 1
  • 1
6

If the node_modules directory that contains the script you want to import is not at the root of your VSCode workspace, you can point the solidity extension to it manually in .vscode/settings.json like so:

{
  "solidity.packageDefaultDependenciesDirectory": "path/to/sub/dir/node_modules"
}
Chris Drifte
  • 161
  • 1
  • 4
5

If you're using VSCode, this error is caused when your IDE fails to resolve the import paths.

Some contract packages contain contracts in the contracts folder, whereas others may contain subfolders containing contracts folders in them, and this causes path errors.

If you're using Solidity extension by Juan, make sure you have your remappings correct:

Solidity Remappings

This is an example of the settings.json file that would pop up if you choose to modify the remappings. Note that the remapping template is: NAME_OF_PACKAGE/=node_modules/PATH_TO_PACKAGE:

{
   ...,
   "solidity.remappingsUnix": [
        "@uniswap/=node_modules/@uniswap/",
        "@openzeppelin/=node_modules/@openzeppelin/"
   ]
}
Sohail Saha
  • 483
  • 7
  • 17
5

The error is caused by the solc-js compiler. The GitHub page is https://github.com/ethereum/solc-js

You need to compile with an import callback, I do not know how Truffle handles this, but in case you were compiling yourself programmatically, you would have to use an import callback as in the following code (example taken from the GitHub page, the findImports function changed to how it is working for me):

const solc = require('solc');

const input = {
  language: 'Solidity',
  sources: {
    'test.sol': {
      content: 'import "lib.sol"; contract C { function f() public { 
L.f(); } }'
    }
  },
  settings: {
    outputSelection: {
      '*': {
        '*': ['*']
      }
    }
  }
};

function findImports(relativePath) {
  //my imported sources are stored under the node_modules folder!
  const absolutePath = path.resolve(__dirname, 'node_modules', relativePath);
  const source = fs.readFileSync(absolutePath, 'utf8');
  return { contents: source };
}

// New syntax (supported from 0.5.12, mandatory from 0.6.0)
var output = JSON.parse(
  solc.compile(JSON.stringify(input), { import: findImports })
);
toongeorges
  • 1,844
  • 17
  • 14
3

Create a folder .vscode in your root folder and then create a file settings.json inside .vscode with the following content. Ensure the path is correct:

{
    "solidity.remappings":["@openzeppelin/=/Users/john/workspace/myproject/smart_contract/node_modules/@openzeppelin"] 
}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
  • This worked for me, but simply as a side note: I could use the relative path instead of the absolute path as shown. – Jesper - jtk.eth Aug 26 '22 at 00:16
  • You can add these lines to add support for all remappings: `{ "solidity.packageDefaultDependenciesContractsDirectory": "src", "solidity.packageDefaultDependenciesDirectory": "lib" }` – BonisTech Mar 19 '23 at 19:45
2

Install any missing dependencies and add them to your package.json.
Note that some packages, like @chainlink/contracts require using yarn, because they use yarn workspaces.

npm ERR! Error: Please use yarn to install dependencies

for example:

yarn add @chainlink/contracts

However, I did not make it work for packages that include @version tag, because the import path does not match any folder in node_modules.

npm i @openzeppelin/contracts@4.4.0

enter image description here The error went away when I removed the version from the path, but I don't know how legit this is. enter image description here It does still compile though ¯\(ツ)

Qwerty
  • 29,062
  • 22
  • 108
  • 136
1

I am running hardhat in a yarn package, under packages/. To eliminate this error, go to the preferences for the Solidity plugin.

Preference: Package Default Dependencies Directory

Value: packages/hardhat/node_modules

enter image description here

Duke
  • 7,070
  • 3
  • 38
  • 28
1

For me, the following worked

Under solidity plugin settings on vs code(I'm on mac), I made sure that node_modules is removed from Solidity: Package Default Dependencies Directory box.

As soon as I remove this, the error goes away.

If I add node_modules back in that box, the error comes again.

enter image description here

PS: I am assuming that in your repo directory, you have already installed openzeppelin correctly

npm install @openzeppelin/contracts

rajya vardhan
  • 1,121
  • 4
  • 16
  • 29
1

A simple hack to this would be import from the absolute path of the module. Something like import "/yourApp/node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol" in your .sol file! This is hassle free and works 100% :)

Though the above will remove the error from VSCODE but when you will compile the contract it will throw errors. So it would be better to be with the VSCODE error and get the contract compiled and deployed without errors!! :D

0

ERC20 file requires other files

pragma solidity ^0.8.0;

import "./IERC20.sol";
import "./extensions/IERC20Metadata.sol";
import "../../utils/Context.sol";

it is not clear if you have correctly installed OpenZeppelin or not.

0

If you are using VSCODE solidity extension: make sure you are running VSCODE from the directory below /contracts/ and /node_modules/ where the package.json lives.

Paths will be updated and the errors will go away.

  • I'm dealing with the same issue... can you go further into detail? I see the packages in the package.json and node modules and i'm getting the same error? – thomas305 Jan 25 '22 at 17:27
0

Before trying anything, in case any of you copied the entire contract from a tutorial, first try changing the contract 'Name'. For me, I noticed I still had the original contract name here, and once I changed it to MY project (contract) name, then error went away. Worth a shot before tinkering with the extension settings:

E.g. Instead of:

contract OriginalTutorialContractName is ERC721, Ownable {

make sure to update for your project:

contract YourContractNameHere is ERC721, Ownable {

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Apr 30 '22 at 15:02
0

I am on Linux working with a truffle project. I passed the relative path even though node_modules is automatically set as the Package Default Dependencies Contracts Directory setting of the solidity extension:

 import "../node_modules/@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol";
 import "../node_modules/@openzeppelin/contracts/utils/Counters.sol";
Yilmaz
  • 35,338
  • 10
  • 157
  • 202
0

If u have installed the extension and tried importing from node modules, then try to restart VS, this will refresh the cache and fix it.

Bharat Singh
  • 93
  • 1
  • 12
0

I had a similar solution to @APerson1000 in this thread, except I needed to remove the node_modules reference in the Solidity extension settings in VSCode, in particular the Solidity: Package Default Dependencies Directory field.

Barney Chambers
  • 2,720
  • 6
  • 42
  • 78
0

After countless hours, I noticed I had a package.json or a hardhat.config.js upward in the hierarchy's repo, due to a "npx hardhat" or "yarn" in the wrong repo. Deleting the configs files, the node_modules upward, following by a reboot solved my case !

If the error still persist, just copy paste your repo directly in your home repo and give it a try here. If the error disappear you have to find the malicious config file somewhere in your path...

Fabien
  • 97
  • 8
0

In my case, I forgot installing openzeppelin contract.

Install openzeppelin contract by running-

npm install @openzeppelin/contracts

Confirm workspace compiler is set to localNodeModule

Note - Reloading vs code window might help too.

-1

I resolved it by changing vscode solidity extension version to v0.0.135.

enter image description here

rahul_eth
  • 123
  • 3