Can't import openzeppelin node_modules: File import callback not supported
Asked Answered
P

9

9

Whenever I try to compile my solidity contract, the error ParserError: Source \"@OpenZeppelin/contracts/math/SafeMath.sol\" not found: File import callback not supported gets thrown.

pragma solidity ^0.7.0;

import "@OpenZeppelin/contracts/token/ERC20/ERC20.sol";
import "@OpenZeppelin/contracts/math/SafeMath.sol";

Any ideas what could be causing this?

Palomino answered 30/1, 2021 at 15:8 Comment(4)
are you using VSCode plugin to compile it?Mortal
See my solidity node_module referencing here https://mcmap.net/q/418238/-file-import-callback-not-supportedMagazine
Does this answer your question? File import callback not supported?Industrial
If you read the VS Code extension docs (I am using the extension by Juan Blanco), it has a section about openzepplin. You need to edit your settings and add the following (assuming node_modules is in the root dir of your ptoject) : "solidity.packageDefaultDependenciesContractsDirectory": "", "solidity.packageDefaultDependenciesDirectory": "node_modules"Savick
L
9

Make sure you opened VS Code in the root directory of the project.

In my case, this is how my VS Code directory looked like when I get that same error.

projects
 |___MyContract
   |__contracts
     |__Contract.sol

Reopening VS Code in the MyContract directory fixes it.

MyContract
 |__contracts
   |__Contract.sol
Lucinalucinda answered 2/9, 2021 at 7:3 Comment(1)
This fixed it for me, was using a project cloned from github.Melanoid
R
8

I have the same problem right now. I'm using truffle+node and I fixed it modifying the import path to a relative path, for example,

pragma solidity ^0.7.0;

import "../node_modules/OpenZeppelin/contracts/token/ERC20/ERC20.sol";
import "../node_modules/OpenZeppelin/contracts/math/SafeMath.sol";

I'm not 100% sure why this happen but I hope I helped you.

Rivet answered 1/2, 2021 at 21:26 Comment(0)
B
8

After installing OpenZeppelin close your IDE and re-open it.

Bruges answered 19/7, 2021 at 14:34 Comment(2)
You're right, hahaha. The Solidity plugin for VSCode is to blame, I suppose. I edit: the annoying error didn't go away.Bobbyebobbysocks
@Bobbyebobbysocks make sure the import path is correctBruges
O
2

For some environments, SafeMath has a different path

import "@openzeppelin/contracts/utils/math/SafeMath.sol";
Othello answered 10/5, 2021 at 11:57 Comment(1)
This was the solution for me.Aguste
P
1

what worked for me; (I am using Windows10 and visual studio code) -click on the extensions icon -search for the solidity extension -select the uninstall button and click on the drop down arrow -select install another version -select version 0.0.135 -After the installation, click on "Reload required"

Polygon answered 8/3, 2022 at 13:4 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Whoremaster
I had a similar bug with version 0.0.163 installed and switching to a previous version fixed the issue(I switched to v0.0.153).Autophyte
J
1

Make sure you installed openzeppelin

npm install --save @openzeppelin/[email protected]
Japeth answered 7/4, 2022 at 5:22 Comment(0)
C
0

actualy the problem is solidity compiler is stupid and doesn't automatically follow path, you have to specify all allowed path manually (https://docs.soliditylang.org/en/latest/using-the-compiler.html), every each one of them and completely to the file(cannot give juste the higher level folder) use npx hardat compile if you can it's better

Cedar answered 4/6, 2021 at 23:23 Comment(0)
K
0

This worked for me -

Change the settings in Solidity extension in VSCode.

The default settings include: "solidity.packageDefaultDependenciesContractsDirectory": "contracts" Users need to change the setting from contracts to an empty string. "solidity.packageDefaultDependenciesContractsDirectory": ""

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

Krusche answered 1/5, 2022 at 16:53 Comment(0)
B
0

Try this:

import "{Project Name}/node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
Brentonbrentt answered 12/2, 2023 at 16:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.