0

I'm trying to use the @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 exists now in the node_modules folder. I also controlled the package.json file is 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 follows:

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.

Ismaili Mohamedi
  • 906
  • 7
  • 15
SaMLi
  • 1
  • 1

1 Answers1

1

Unfortunaly, @openzeppelin/contracts can't be used as a Node.js library. It is supposed to be installed and used into a .sol file. For exemple:

Exemple.sol

pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract Exemple is ERC721 {
    constructor() ERC721("Exemple", "EX") {
  }
}

Then it should be build by an Ethereum development environment like HardHat to get access to the ABI in a Exemple.json file.

devpolo
  • 2,487
  • 3
  • 12
  • 28