File import callback not supported?
Asked Answered
H

20

36

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

Haggle answered 29/4, 2021 at 16:5 Comment(3)
any luck? same boat hereComical
Try deleting the Build Artifacts (.json files) and run the command again.Exuviae
Can you specify the IDE you're using?Woodenhead
H
67

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

Hogle answered 20/7, 2021 at 18:26 Comment(4)
Not working for me as wellAnthonyanthophore
For me changing it to localFile did the trickCyclothymia
That was it, thanksNourish
In some files this solution worked but for one file I had to choose localFile to make it work.Caius
S
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

Starve answered 6/1, 2022 at 22:26 Comment(0)
H
8

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"
}
Hazelton answered 14/2, 2022 at 10:33 Comment(0)
P
7

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 })
);
Peel answered 14/5, 2022 at 14:31 Comment(0)
L
6

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/"
   ]
}
Lorrin answered 9/5, 2022 at 7:31 Comment(0)
S
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"] 
}
Saddlebow answered 22/3, 2022 at 13:28 Comment(2)
This worked for me, but simply as a side note: I could use the relative path instead of the absolute path as shown.Eisegesis
You can add these lines to add support for all remappings: { "solidity.packageDefaultDependenciesContractsDirectory": "src", "solidity.packageDefaultDependenciesDirectory": "lib" }Woodenhead
B
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/[email protected]

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 ¯\(ツ)

Bonni answered 22/12, 2021 at 5:25 Comment(0)
B
2

I solve this issue by setting extension config solidity.monoRepoSupport to false (default is true).

Biweekly answered 8/5 at 5:27 Comment(0)
C
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

Corsica answered 4/2, 2022 at 21:31 Comment(0)
C
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

Coeternal answered 13/5, 2022 at 19:28 Comment(0)
M
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

Marcelina answered 8/6, 2022 at 13:13 Comment(0)
H
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.

Hamsun answered 22/5, 2021 at 21:7 Comment(0)
L
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.

Longsufferance answered 24/1, 2022 at 20:59 Comment(1)
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?Ventricose
L
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 {

Lengthy answered 28/4, 2022 at 17:53 Comment(1)
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.Specific
R
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";
Rea answered 6/5, 2022 at 19:3 Comment(0)
F
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.

Flavine answered 25/8, 2022 at 13:55 Comment(0)
R
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.

Redeem answered 29/10, 2022 at 19:15 Comment(0)
A
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...

An answered 28/3, 2023 at 15:49 Comment(0)
I
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.

Implacental answered 4/8, 2023 at 7:9 Comment(0)
C
-1

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

enter image description here

Comte answered 13/7, 2022 at 19:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.