Error importing OpenZeppelin contracts in vscode

Hi,

I was following the OpenZepellin doc on developing smart contracts and after installing truffle (in my current working directory) with command

npx truffle init

I then tried installing the OpenZeppelin contracts library (also in my current working directory) with command

npm install --save-dev @openzeppelin/contracts

it seemed like it worked, but then when I try importing the Ownable contract in my contracts folder in VSCode, with the command

import "@openzeppelin/contracts/access/Ownable.sol";

i get this

Source "@openzeppelin/contracts/access/Ownable.sol" not found: File import callback not supported

Thing is, I can’t even see a folder showing the several solidity files for the OpenZeppelin contracts library in my folder, so I’m not even sure where it was installed.

Any idea what I might have done wrong?

I think if you have installed @openzeppelin/contracts successfully, this package should show at:

-Your Contract Project
  -node_modules
    -@openzeppelin
      -contracts
        -access
          -Ownable.sol
1 Like

hi,

I don’t have this package in my project. I’m not sure what else to do as it seemed to have installed successfully.

I am not sure whether you need a package.josn at first, then you can run npm install --save-dev @openzeppelin/contracts, if you have installed successfully, the dependency @openzeppelin should be in the node_modules

image

2 Likes

it says this btw, not sure what it means

chika@Chikas-MacBook-Pro cryptoTomb % npm install --save-dev @openzeppelin/contracts
npm WARN chika@1.0.0 No description
npm WARN chika@1.0.0 No repository field.

+ @openzeppelin/contracts@4.0.0
updated 1 package and audited 1 package in 0.604s
found 0 vulnerabilities
1 Like

So how about running truffle compile now?

1 Like

Hi @ch1ka,

Try creating a package.json using npm init -y: https://docs.openzeppelin.com/learn/setting-up-a-node-project

Hi @ch1ka , you should change

Solidity: Package Default Dependencies Directory

on your plugin to {ur project folder}/node_modules

1 Like

Just went to check what @ZenoHwanEth said, even tried adding ./ but didn't work, I keep seeing how the tutorials don't have it and it keep bothering me

I also have said contracts in node_modules

1 Like

Are you using yarn workspaces? If so your zeppelin contract may be being hoisted to the top level node_modules. That was the issue for me

Hi, had the same issue as you, simply need to redirect to the right folder before running npm install so do this:

in your terminal enter cd followed by the projects path

then run npm install @openzeppelin/contracts

Its always the very simple things that take up most of our time, thats why its good to always have someone to work with. Two people are less likely to make the same mistake