错误:找不到模块‘@openzeppelin/contracts

huangapple go评论63阅读模式
英文:

Error: Cannot find module ‘@openzeppelin/contracts

问题

我正在尝试在我的JS代码中使用@openzeppelin库该库将用于在goerli测试网络中铸造代币
我正在使用Infura作为API
我使用命令npm install @openzeppelin/contracts@openzeppelin/contracts安装到node_modules中我检查它现在存在于node_modules文件夹中
我还检查了package.json文件也存在如下"@openzeppelin/contracts": "^4.8.2"
当我运行代码时出现了以下错误

错误: 找不到模块'@openzeppelin/contracts'
要求堆栈:

  • C:\Users\mamou\Desktop\NFT\evaluation\test.js
    在 Module._resolveFilename (node:internal/modules/cjs/loader:1075:15)
    在 Module._load (node:internal/modules/cjs/loader:920:27)
    在 Module.require (node:internal/modules/cjs/loader:1141:19)
    在 require (node:internal/modules/cjs/helpers:110:18)
    在 Object. (C:\Users\mamou\Desktop\NFT\evaluation\test.js:3:20)
    在 Module._compile (node:internal/modules/cjs/loader:1254:14)
    在 Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
    在 Module.load (node:internal/modules/cjs/loader:1117:32)
    在 Module._load (node:internal/modules/cjs/loader:958:12)
    在 Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12) {
    code: 'MODULE_NOT_FOUND',
    requireStack: [ 'C:\Users\mamou\Desktop\NFT\evaluation\test.js' ]
    }

我的代码如下:
```javascript
const { ethers } = require("ethers");
//在运行时出现MODULE_NOT FOUND或意外的标识符问题
const { ERC721 } = require("@openzeppelin/contracts");
const { NodeHttpProvider } = require("ethers/providers");

// 设置提供者和签名者
const provider = new NodeHttpProvider("https://goerli.infura.io/v3/我的API密钥");
const signer = new ethers.Wallet("我的私钥", provider);

// 设置ERC721合约实例
const contractAddress = "我的合约地址";
const erc721Contract = new ethers.Contract(contractAddress, ERC721.abi, signer);

// 铸造一个新代币
const tokenId = 1;
const tokenURI = "https://ipfs.io/ipfs/QmR34NuvQUrFVyq4uLtb4uvN2HnsNjUetmv6JYraHFsFxU?filename=nft.json";
erc721Contract.mint(signer.address, tokenId, tokenURI)
  .then((receipt) => {
    console.log(`代币铸造成功。交易哈希:${receipt.hash}`);
  })
  .catch((err) => {
    console.error(err);
  });

你能帮我找到问题吗?
提前谢谢你。


<details>
<summary>英文:</summary>

I&#39;m trying to use the @openzeppelin library in my JS code which will be used to mint a token to goerli test net.
I&#39;m using Infura as API.
I installed @openzeppelin/contracts to node_modules using the command &quot; npm install @openzeppelin/contracts&quot; I checked it exists now in the node_modules folder.
I also controlled the package.json file is also existed as &quot;@openzeppelin/contracts&quot;: &quot;^4.8.2&quot;,
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.

</details>


# 答案1
**得分**: 1

抱歉,`@openzeppelin/contracts` 不能作为 `Node.js` 库使用。它应该被安装并在 `.sol` 文件中使用。例如:

**Exemple.sol**
```sol
pragma solidity ^0.8.0;

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

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

然后,它应该由一个像 HardHat 这样的 以太坊开发环境 构建,以便在 Exemple.json 文件中获得对 ABI 的访问。

英文:

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 &quot;@openzeppelin/contracts/token/ERC721/ERC721.sol&quot;;

contract Exemple is ERC721 {
    constructor() ERC721(&quot;Exemple&quot;, &quot;EX&quot;) {
  }
}

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

huangapple
  • 本文由 发表于 2023年3月31日 21:23:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75899045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定