My compiler is not recognizing my import. Don't hate me for posting this again. I've clicked through about 10 different forum posts similar to this one but none actually has solved my particular issue after trying.
Environment
The latest truffle version. I'm using the latest OpenZeppelin code that is downloaded from NPM.
Details
Long story short I'm new and I've been developing a project that compiles fine before I add an import statement to one of the "@openZeppelin" contracts. I checked the settings and `Solidity: Package Default Dependencies Directory = "node_modules"`
Solidity: Package Default Dependencies Contracts Directory = "" I changed the file structure a few times and I ended up moving node_modules into the same folder as the smart contract I’m importing “@openzeppelin” from and it’s still not working.
Here’s the path I’m using
To keep your system secure, you should always use the installed code as-is, and neither copy-paste it from online sources, nor modify it yourself.
_From: https://docs.openzeppelin.com/contracts/3.x/#usage_
See the documentation on how to use and import the upgradeable version of OpenZeppelin Contracts:
What did you want to change in node_modules?
Feel free to share a cut down version of your contract.
Hello there. I was getting the same import error while trying to compile .sol.
I’m using VSCode + solidity plugin.
The problem here was that I had multiple projects on the same workspace. And even when I changed the workspace compiler version to Remote and set the correct version for the project, I got this error:
The @openzeppelin was download correctly as a dependency in my project /node_modules folder. But the compiler was using the rootWorkspace dir as it’s “root”, and I got no /node_modules there.
The interesting was that I could compile using the truffle or hardhat, so it should be something related to relative paths used by the plugin’s compiler and where it was looking for the dependencies.
So I opened up just only that one project I was interested working on, to test if now the VSCode solidity plugin would work, and Eureka!
It worked!
I hope you get your project up and running again!
For whom wants to go a little deeper, here is the result of a brief analysis I did on the reasons for that:
I was intrigued by this behavior, so I took a look into the Solidity Extension source code, and I think I’ve find how to fix it, but I need more time for that.
I never had develop a VSCode extension before, but my intuition tells me that the plugin should using
This is the way the plugin is accessing the definitions for the workspace for every time someone tries to compile the code. But it breaks if you have multiple projects using different solidity versions.
I fixed it. for some reason, it needed me to include the root path to the node_modules setting.
The reason is that the extension by default expects node_modules in the root directory of your project. If it's somewhere else, you have to specify the path. Truffle has an elaborate path resolver which can detect it which is why it works. I think the extension does not have anything like that and just relies on you giving it the right paths.
Hi, @natanloterio! I've had an issue just trying to install OpenZeppelin contracts in the first place. I typed the npm install 3 different ways:
npm i @openzeppelin/contracts
npm install @openzeppelin/contracts@v3.0.0
npm install @openzeppelin/contracts@v3.0.0-beta.0
(I was choosing 3.0.0 because that was the version my instructor was using in
one of my Udemy courses)
And my terminal would act like it was installing it, but it usually spit out a bunch of errors about dependencies that needed fixing (which is normal). I tried to run npm audit fix, and that did not help. Neither did npm audit fix --force.
What did you type to install it? And did you have to open any empty folders or anything to get it right? I don't understand why VS Code doesn't want to install OpenZeppelin. Or have the imports done for the right node_modules folders.
BTW, when I run the npm install, it'll sometimes show up in my mode_modules folder for my entire computer. I need a node_modules folder for my project directory, and it won't make one. Why is that? Thanks for your help in advance, @natanloterio (or anyone who wants to help).
Seems to be the main issue (there's no other open issue about that right now) but I think that creating a new issue with a good description and focusing on the problem you're having here would not hurt. Right now the issue is a bit fuzzy - it's about linting but not really and it's not clear what does and what does not work with multi-root workspaces from user's perspective. The maintainer might not even be aware of all the problems you're having. Also, giving a good use case and showing how much it affects users always helps to better prioritize the issue.