[typescript] Next.js & Typescript 에서 하드햇(Hardhat) 테스트 에러 (An unexpected error occurred)
Next js / 타입스크립트 / Hardhat / config / 모듈
1. Next js 를 타입스크립트로 개발환경 만들기
npx create-next-app --typescript nft-marketplace
👈 명령어
2. Next js에 하드햇(hardhat) 개발환경 만들기
npx hardhat
👈 명령어
3. 하드햇 test를 위한 모듈 설치
테스트를 돌려보려면 필요한 npm 모듈을 설치해줘야한다
- @nomicfoundation/hardhat-network-helpers
- @nomicfoundation/hardhat-chai-matchers
- @nomicfoundation/hardhat-toolbox
- @nomiclabs/hardhat-waffle
- @types/mocha
- ts-node
- chai
- ethers
npm install --save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers ts-node @types/mocha @nomicfoundation/hardhat-toolbox @nomicfoundation/hardhat-network-helpers @nomicfoundation/hardhat-chai-matchers @nomiclabs/hardhat-ethers @nomiclabs/hardhat-etherscan chai ethers hardhat-gas-reporter solidity-coverage @typechain/hardhat typechain @typechain/ethers-v5 @ethersproject/abi @ethersproject/providers
👈 명령어
4. 하드햇 테스트
npx hardhat test
👈 명령어
- An unexpected error occurred: 이라는 에러가 발생 ❗
5. tsconfig.json 설정변경
Next js 로 자동설정 되어있던 ‘module’ 부분을
esnext
👉commonjs
으로 변경
6. 수정 후 테스트
npx hardhat test
👈 명령어
정상적으로 test 완료가 되는걸 확인 할 수 있다!
참고 했던 사이트 🖥
https://dev.to/chiranz/comment/1elh9
발생했던 에러 내용
An unexpected error occurred: C:\Users\waves\Downloads\nft-marketplace\hardhat.config.ts:1 import “@nomicfoundation/hardhat-toolbox”; ^^^^^^ SyntaxError: Cannot use import statement outside a module at Object.compileFunction (node:vm:360:18) at wrapSafe (node:internal/modules/cjs/loader:1088:15) at Module._compile (node:internal/modules/cjs/loader:1123:27) at Module.m._compile (C:\Users\waves\Downloads\nft-marketplace\node_modules\ts-node\src\index.ts:1618:23) at Module._extensions..js (node:internal/modules/cjs/loader:1213:10) at Object.require.extensions.
[as .ts] (C:\Users\waves\Downloads\nft-marketplace\node_modules\ts-node\src\index.ts:1621:12) at Module.load (node:internal/modules/cjs/loader:1037:32) at Function.Module.\_load (node:internal/modules/cjs/loader:878:12) at Module.require (node:internal/modules/cjs/loader:1061:19) at require (node:internal/modules/cjs/helpers:103:18)