开发者

Can't import OpenZeppelin contracts when using Yarn package manager and Truffle

I'm writing my first smart contract and I'm trying a few new things at once. One of which is using Yarn 3 (which apparently works very different from the older versions).

When running yarn truffle compile, I get this error:

Using network 'test'.


Compiling your contracts...
===========================
> Compiling ./contracts/HelloWorld.sol

CompileError: ParserError: Source "@openzeppelin/contracts/utils/Strings.sol" not found
 --> project:/contracts/HelloWorld.sol:5:1:
  |
5 | import "@openzeppelin/contracts/utils/Strings.sol";
  | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Compilation failed. See above.
    at /Users/craig/Code/ethereum-hello-world/.yarn/unplugged/truffle-npm-5.6.8-9c74c1574e/node_modules/truffle/build/webpack:/packages/compile-solidity/dist/run.js:95:1
    at Generator.next (<anonymous>)
    at fulfilled (/Users/craig/Code/ethereum-hello-world/.yarn/unplugged/truffle-npm-5.6.8-9c74c1574e/node_modules/truffle/build/webpack:/packages/compile-solidity/dist/run.js:28:43)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)

When I remove this line, it compiles just fine:

import "@openze开发者_如何学编程ppelin/contracts/utils/Strings.sol";

This is my package.json:

{
  "name": "ethereum-hello-world",
  "version": "0.0.1",
  "packageManager": "yarn@3.3.0",
  "scripts": {
    "test": "mocha"
  },
  "dependencies": {
    "truffle": "^5.6.8"
  },
  "devDependencies": {
    "@openzeppelin/contracts": "^4.8.0",
    "@openzeppelin/test-helpers": "^0.5.16",
    "chai": "^4.3.7"
  }
}

(To add the openzeppelin/contracts above, I used yarn add -D @openzeppelin/contracts.)

This is my HelloWorld.sol:

// SPDX-License-Identifier: ISC
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/utils/Strings.sol";

contract HelloWorld {
    string public text = "Hello World";

    constructor() {
        // Do nothing
    }
}

What am I doing wrong?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜