Error: Cannot find module '@openzeppelin/contracts'

Hello I'm trying to use @openzeppelin library in my JS code which will be used to mint a token to goerli test net.
I'm using Infura as API.
I installed @openzeppelin/contracts to node_modules using the command " npm install @openzeppelin/contracts" I checked it is existed now in node_modules folder.
I also controled package.json file it iss also existed as "@openzeppelin/contracts": "^4.8.2",
when I run the code I have the following error:

Error: Cannot find module '@openzeppelin/contracts'
Require stack:
- C:\Users\mamou\Desktop\NFT\evaluation\test.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    at Module._load (node:internal/modules/cjs/loader:920:27)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at Object.<anonymous> (C:\Users\mamou\Desktop\NFT\evaluation\test.js:3:20)
    at Module._compile (node:internal/modules/cjs/loader:1254:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Module._load (node:internal/modules/cjs/loader:958:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'C:\\Users\\mamou\\Desktop\\NFT\\evaluation\\test.js' ]

My code is as follow:

const { ethers } = require("ethers");
//Here is the problem while running MODULE_NOT FOUND or unexpected identifier
const { ERC721 } = require("@openzeppelin/contracts");
const { NodeHttpProvider } = require("ethers/providers");

// Set up provider and signer
const provider = new NodeHttpProvider("https://goerli.infura.io/v3/my API key");
const signer = new ethers.Wallet("my private key", provider);

// Set up ERC721 contract instance
const contractAddress = "my contract address";
const erc721Contract = new ethers.Contract(contractAddress, ERC721.abi, signer);

// Mint a new token
const tokenId = 1;
const tokenURI = "https://ipfs.io/ipfs/QmR34NuvQUrFVyq4uLtb4uvN2HnsNjUetmv6JYraHFsFxU?filename=nft.json";
erc721Contract.mint(signer.address, tokenId, tokenURI)
  .then((receipt) => {
    console.log(`Token minted. Transaction hash: ${receipt.hash}`);
  })
  .catch((err) => {
    console.error(err);
  });

can you help me to find the problem?
Thank you from now.

Try this instead:

const { ERC721 } = require("@openzeppelin/contracts/token/ERC721");

Thank you for your help.
Unfortunately I still have the same error

This is not correct.

If you are in a solidity file, trying to import a solidity source you would indeed do

const { ERC721 } = require("@openzeppelin/contracts/token/ERC721");

But in your case you are in a JS file, trying to import an interface/ABI (that would be my guess anyway). You want to do

const ERC721 = require("@openzeppelin/contracts/build/contracts/ERC721.json");
1 Like

Thank you very much now worked