使用yarn hardhat命令创建hardhat项目是报出如下错误
chain % yarn hardhat
yarn run v1.22.19
$ ~/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat
~/VscodeWeb3Projects/hhproject/chain/node_modules/hardhat/internal/cli/cli.js:129
let taskName = parsedTaskName ?? task_names_1.TASK_HELP;
^
SyntaxError: Unexpected token '?'
at Module._compile (internal/modules/cjs/loader.js:891:18)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
at Module.load (internal/modules/cjs/loader.js:811:32)
at Function.Module._load (internal/modules/cjs/loader.js:723:14)
at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)
at internal/main/run_main_module.js:17:11
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
参考文档 https://stackoverflow.com/questions/73622631/npx-hardhat-compile-unexpected-token
应该是node版本的问题
需要升级 nodejs的版本
升级nodejs版本参考文档https://www.jianshu.com/p/acd316dceeb8
# 清除缓存
~ % sudo npm cache clean -f
npm WARN using --force I sure hope you know what you are doing.
╭───────────────────────────────────────────────────────────────╮
│ │
│ New major version of npm available! 6.13.4 → 9.2.0 │
│ Changelog: https://github.com/npm/cli/releases/tag/v9.2.0 │
│ Run npm install -g npm to update! │
│ │
╰───────────────────────────────────────────────────────────────╯
# 使用 npm安装node 模块
~ % sudo npm install -g n
/usr/local/bin/n -> /usr/local/lib/node_modules/n/bin/n
+ n@9.0.1
added 1 package from 2 contributors in 2.451s
# 查看node的所有版本
~ % npm view node versions
# 升级到最新版本,升到具体版本号使用 sudo n xx.xx
~ % sudo n latest
installing : node-v19.3.0
mkdir : /usr/local/n/versions/node/19.3.0
fetch : https://nodejs.org/dist/v19.3.0/node-v19.3.0-darwin-x64.tar.xz
copying : node/19.3.0
installed : v19.3.0 (with npm 9.2.0)
重新执行 yarn hardhat命令,可以正常继续执行并创建项目成功
chain % yarn hardhat
yarn run v1.22.19
warning package.json: No license field
$ /Users/jiangwujie/VscodeWeb3Projects/hhproject/chain/node_modules/.bin/hardhat
You are using a version of Node.js that is not supported by Hardhat, and it may work incorrectly, or not work at all.
Please, make sure you are using a supported version of Node.js.
To learn more about which versions of Node.js are supported go to https://hardhat.org/nodejs-versions
888 888 888 888 888
888 888 888 888 888
888 888 888 888 888
8888888888 8888b. 888d888 .d88888 88888b. 8888b. 888888
888 888 "88b 888P" d88" 888 888 "88b "88b 888
888 888 .d888888 888 888 888 888 888 .d888888 888
888 888 888 888 888 Y88b 888 888 888 888 888 Y88b.
888 888 "Y888888 888 "Y88888 888 888 "Y888888 "Y888
👷 Welcome to Hardhat v2.12.5 👷
# 选择 TypeScript项目
✔ What do you want to do? · Create a TypeScript project
✨ Project created ✨
See the README.md file for some example tasks you can run
Give Hardhat a star on Github if you're enjoying it! 💞✨
https://github.com/NomicFoundation/hardhat
Please take a moment to complete the 2022 Solidity Survey: https://hardhat.org/solidity-survey-2022
✨ Done in 332.94s.
参考文档:
- https://stackoverflow.com/questions/73622631/npx-hardhat-compile-unexpected-token
- https://www.jianshu.com/p/acd316dceeb8