SyntaxError: Identifier expected. 'const' is a reserved word that cannot be used here. 에러 해결
원래 하드햇 기본 세팅대로 자동 생성된 hardhat confing.ts는 다음과 같다.
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
const config: HardhatUserConfig = {
solidity: "0.8.19",
};
export default config;
하지만 만약 ethers v6 를 사용할 경우 type-chain과정에서 다음과 같은 에러가 발생한다.
Generating typings for: 19 artifacts in dir: typechain-types for target: ethers-v6
An unexpected error occurred:
SyntaxError: Identifier expected. 'const' is a reserved word that cannot be used here. (4:18)
2 | /* tslint:disable */
3 | /* eslint-disable */
> 4 | import type * as const from './const';
| ^
5 | export type { const };
6 | import type * as interfaces from './interfaces';
7 | export type { interfaces };
at v (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/parser-typescript.js:1:14679)
at _H (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/parser-typescript.js:49:10722)
at Object.cH [as parse] (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/parser-typescript.js:49:11028)
at Object.parse (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/index.js:7515:23)
at coreFormat (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/index.js:8829:18)
at formatWithCursor2 (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/index.js:9021:18)
at /Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/index.js:38183:12
at Object.format (/Users/chohk/Desktop/git/web2x-vote/node_modules/prettier/index.js:38197:12)
at prettierOutputTransformer (/Users/chohk/Desktop/git/web2x-vote/node_modules/typechain/src/codegen/outputTransformers/prettier.ts:8:19)
at /Users/chohk/Desktop/git/web2x-vote/node_modules/typechain/src/typechain/io.ts:22:33 {
loc: { start: { line: 4, column: 18 } },
codeFrame: '\x1B[0m \x1B[90m 2 |\x1B[39m \x1B[90m/* tslint:disable */\x1B[39m\x1B[0m\n' +
'\x1B[0m \x1B[90m 3 |\x1B[39m \x1B[90m/* eslint-disable */\x1B[39m\x1B[0m\n' +
"\x1B[0m\x1B[31m\x1B[1m>\x1B[22m\x1B[39m\x1B[90m 4 |\x1B[39m \x1B[36mimport\x1B[39m type \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m \x1B[36mconst\x1B[39m \x1B[36mfrom\x1B[39m \x1B[32m'./const'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n" +
'\x1B[0m \x1B[90m |\x1B[39m \x1B[31m\x1B[1m^\x1B[22m\x1B[39m\x1B[0m\n' +
'\x1B[0m \x1B[90m 5 |\x1B[39m \x1B[36mexport\x1B[39m type { \x1B[36mconst\x1B[39m }\x1B[33m;\x1B[39m\x1B[0m\n' +
"\x1B[0m \x1B[90m 6 |\x1B[39m \x1B[36mimport\x1B[39m type \x1B[33m*\x1B[39m \x1B[36mas\x1B[39m interfaces \x1B[36mfrom\x1B[39m \x1B[32m'./interfaces'\x1B[39m\x1B[33m;\x1B[39m\x1B[0m\n" +
'\x1B[0m \x1B[90m 7 |\x1B[39m \x1B[36mexport\x1B[39m type { interfaces }\x1B[33m;\x1B[39m\x1B[0m'
}
해당 오류는 린터와 관련된 오류 같은데 아무리 찾아도 검색이 안된다. 다만 "typechain-types for target: ethers-v6" 해당 문구를 보았을때 ethers v6 그리고 type-chain이 키워드인 오류인 것을 볼 수 있었다.
최근
"@nomicfoundation/hardhat-toolbox"
해당 모듈이 잘 안먹히는 케이스가 많기 때문에 따로 ethers와 typechain을 받아와 보면 해결이 가능할까 싶어서
다음과 같이 ethers-v6 모듈을 따로 받아서 하니까 컴파일이 되었다.
import "ethers";
import "@typechain/ethers-v6";
export default {
solidity: "0.8.23",
};
해당 모듈의 경우 다음 링크를 통해 설치가 가능하다.
https://www.npmjs.com/package/@typechain/ethers-v6
@typechain/ethers-v6
🔌 TypeChain target for ethers-v6. Latest version: 0.5.1, last published: 3 months ago. Start using @typechain/ethers-v6 in your project by running `npm i @typechain/ethers-v6`. There are 33 other projects in the npm registry using @typechain/ethers-v6.
www.npmjs.com
실제로 위와 같이 해본 결과 사용할 수 있는 코드의 양이 너무 제한적이였다.
그리고 위의 에러는 결국 타입체인에서의 오류가 아닌 파일 경로에 const가 들어가 있어서 린터에서 잡는 에러였다.
*해결방안
경로에 const를 제거하고 다른 이름을 쓰니 오류가 풀렸다. => 오류 메세지가 위와 같이 린터일 경우 경로에 해당 단어가 있는지 살펴보자!