Reputation: 499
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
Upvotes: 37
Views: 40761
Reputation: 11
I changed this in "@ext:JuanBlanco.solidity" config user settings :
"solidity.packageDefaultDependenciesContractsDirectory": "", "solidity.packageDefaultDependenciesDirectory": "node_modules",
it's work for me
Upvotes: 0
Reputation: 470
I solve this issue by setting extension config solidity.monoRepoSupport
to false
(default is true
).
Upvotes: 2
Reputation: 117
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.
Upvotes: 0
Reputation: 107
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...
Upvotes: 0
Reputation: 2783
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.
Upvotes: 0
Reputation: 103
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.
Upvotes: 0
Reputation: 132
I resolved it by changing vscode solidity extension version to v0.0.135.
Upvotes: -1
Reputation: 109
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
Upvotes: 1
Reputation: 1976
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 })
);
Upvotes: 7
Reputation: 1131
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.
PS: I am assuming that in your repo directory, you have already installed openzeppelin correctly
npm install @openzeppelin/contracts
Upvotes: 1
Reputation: 573
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:
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/"
]
}
Upvotes: 6
Reputation: 49661
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";
Upvotes: 0
Reputation: 41
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 {
Upvotes: 0
Reputation: 31
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"]
}
Upvotes: 3
Reputation: 181
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"
}
Upvotes: 8
Reputation: 7442
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
Upvotes: 1
Reputation: 1
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.
Upvotes: 0
Reputation: 91
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
Upvotes: 9
Reputation: 32018
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]
The error went away when I removed the version from the path, but I don't know how legit this is.
It does still compile though ¯\(ツ)/¯
Upvotes: 2
Reputation: 1076
The Error:
Source "@openzeppelin/contracts/token/ERC20/ERC20.sol" not found: File import callback not supported
Install the Solidity Extention
Select localNodeModule
Might have to restart the IDE
Upvotes: 69
Reputation: 101
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.
Upvotes: 0