hardhat icon indicating copy to clipboard operation
hardhat copied to clipboard

Error HH19: Your project is an ESM project (you have "type": "module" set in your package.json) but your Hardhat config file uses the .js extension.

Open CryptoGraffe opened this issue 1 year ago • 23 comments

Version of Hardhat

2.19.4

What happened?

In trying to move my project to ESM, node20, vite, and other upgrades, I am completely stuck on run hardhat. It gives me a confounding error that the config file ends in .js, when it clearly ends in .ts. I've tried a lot of debugging and can't get anywhere.

Here is my error:

❯ HARDHAT_VERBOSE=true npx hardhat
  hardhat:core:vars:varsManager Creating a new instance of VarsManager +0ms
  hardhat:core:vars:varsManager Loading ENV variables if any +0ms
  hardhat:core:config Loading Hardhat config from /Users/x/dev/yz/hardhat/hardhat.config.ts +0ms
Error HH19: Your project is an ESM project (you have "type": "module" set in your package.json) but your Hardhat config file uses the .js extension.

Rename the file to use the .cjs to fix this problem.

For more info go to https://hardhat.org/HH19 or run Hardhat with --show-stack-traces

I don't have a hardhat.config.js anywhere. If I do a build, one shows up in the ./dist dir but I can delete that and get same error.

Here is my tsconfig.json:

{
  "compilerOptions": {
    "declaration": true,
    "target": "ESNext",
    "module": "esnext",
    "allowJs": true,
    "strict": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "resolveJsonModule": true,
    "esModuleInterop": true,
    "outDir": "dist",
    "skipLibCheck": true,
    "rootDirs": ["./scripts"],
    "isolatedModules": true,
    "lib": ["esnext"]
  },
  "include": ["./typechain", "./deploymentInfo.json","hardhat.config.ts"],
  "exclude": ["node_modules", "dist"],
}

Minimal reproduction steps

I don't have a great reproduction mechanism yet, but can try to provide one if there are no ideas here.

Search terms

No response

CryptoGraffe avatar Jan 15 '24 06:01 CryptoGraffe

After submitting I see that TS isn't supported. I'm using TS+Node20. I tried the workaround that came out Dec 5 and it didn't work, here: https://github.com/NomicFoundation/hardhat/issues/3385#issuecomment-1891392542

CryptoGraffe avatar Jan 15 '24 06:01 CryptoGraffe

@CryptoGraffe I don't think we can help without some way to reproduce the issue. Can you share a link to your repo if it's public?

fvictorio avatar Jan 15 '24 10:01 fvictorio

@fvictorio

git clone [email protected]:coordinape/coordinape.git
cd coordinape
git checkout vite
nvm use 20
git submodule init
cd hardhat
pnpm install
HARDHAT_VERBOSE=true npx hardhat

...

Done in 7.2s
  hardhat:core:vars:varsManager Creating a new instance of VarsManager +0ms
  hardhat:core:vars:varsManager Loading ENV variables if any +1ms
  hardhat:core:config Loading Hardhat config from /Users/x/tmp/coordinape/hardhat/hardhat.config.ts +0ms
Error HH19: Your project is an ESM project (you have "type": "module" set in your package.json) but your Hardhat config file uses the .js extension.

Rename the file to use the .cjs to fix this problem.

For more info go to https://hardhat.org/HH19 or run Hardhat with --show-stack-traces

CryptoGraffe avatar Jan 15 '24 17:01 CryptoGraffe

I am also having the same issue .... any luck ?

dipanshuhappy avatar Jan 26 '24 17:01 dipanshuhappy

@dipanshuhappy I've removed the import of .js module from my hardhat.config.cts and the error is gone.

nikicat avatar Jan 26 '24 17:01 nikicat

@dipanshuhappy I've removed the import of .js module from my hardhat.config.cts and the error is gone.

Got it thanks , this issue can be closed then ?

dipanshuhappy avatar Jan 26 '24 17:01 dipanshuhappy

Getting the same error when trying to use https://github.com/Destiner/abi-coder

mktcode avatar Jan 29 '24 21:01 mktcode

@mktcode can you open a separate issue with steps to reproduce it?

schaable avatar Jan 30 '24 15:01 schaable

EDIT. I think our issue was caused by a dependency override with PNPM. Not sure if this comment is relevant anymore.

Reporting the same error, in my case it only happens when trying to import the @nomicfoundation/hardhat-toolbox 3.0.0 in the project.

Attaching PR for reference: https://github.com/FuelLabs/fuel-bridge/blob/ad7b34ae90ff7fdcdb812403f6d798f8ed94051e/packages/solidity-contracts/hardhat.config.ts#L3

I guess we will be avoiding the toolbox for the time being

To reproduce:

  • pnpm install at the root of the repository
  • Uncomment the line importing hardhat-toolbox at packages/solidity-contracts/hardhat.config.ts
  • cd packages/solidity-contracts && pnpm compile

DefiCake avatar Feb 09 '24 15:02 DefiCake

Posting a summary and reproduction steps for clarity. While others reported similar issues with fixes, the original issue is valid:

Version of Hardhat

2.19.4

What happened?

After migrating to ESM, running Hardhat fails with the error "Error HH19: Your project is an ESM project (you have 'type': 'module' in your package.json) but your Hardhat config file uses the .js extension," but the Hardhat config has the .ts extension.

Minimal reproduction steps

git clone [email protected]:coordinape/coordinape.git
cd coordinape
git checkout vite
nvm use 20
git submodule init
cd hardhat
pnpm install
HARDHAT_VERBOSE=true npx hardhat

schaable avatar Feb 15 '24 13:02 schaable

Same here - upgraded all dependencies and a working typescript project stopped working.

lukastanisic99 avatar Feb 29 '24 18:02 lukastanisic99

I was able to update a couple of Hardhat projects' dependencies to the latest versions by excluding chai. For example, Solidstate uses the latest versions of all nomicfoundation packages, but requires chai 4.x. Although the error messages suggests that this is a Hardhat issue, I don't think that it is.

ItsNickBarry avatar Mar 01 '24 00:03 ItsNickBarry

Yep, I've downgraded chai to 4.3.7 and everything works now

lukastanisic99 avatar Mar 01 '24 01:03 lukastanisic99

hi @ItsNickBarry @lukastanisic99, thanks for the update. Chai 5 only supports ESM, and it's not compatible with the hardhat v2 ESM workaround, as the hardhat config is still CommonJS. OP issue is still valid, as it is using chai 4.x.

schaable avatar Mar 01 '24 14:03 schaable

I research about this issue. The solution that worked for me was to remove this line in the hardhat config file: 'import "@nomicfoundation/hardhat-toolbox";'

nonchain avatar Mar 03 '24 13:03 nonchain

Having the same issue here.

ronak-pal1 avatar Mar 10 '24 16:03 ronak-pal1

I've tried to migrate my newly-created typescript hardhat project to ESM, but switch back to CJS cuz there are some issues can't be solved with ease.

At first, I encountered Unknown file extension ".ts" and solve by using tsx suggested here

But hardhat use ts-node internally, according to Hardhat Document

At the moment, it's not possible to use ESM in TypeScript projects.

Hardhat uses ts-node to run TypeScript projects, which in turn relies on Node's loader hooks. This is all experimental and the current functionality is not enough for Hardhat's needs.

So basically almost all commands (eg. `npx hardhat run') use ts-node indeed.

And for the requirement of using hardhat cli, I have to switch back to ts-node, and there're issues cannot be easily solved by just using ts-node and this and that... emmm that's in a dilemma :S

imckl avatar Mar 16 '24 03:03 imckl

I research about this issue. The solution that worked for me was to remove this line in the hardhat config file: 'import "@nomicfoundation/hardhat-toolbox";'

That fixed it for me too, but the toolbox is required for running tests. Looks like ignition deploy is also broken and returns

pnpm exec hardhat ignition deploy ./ignition/modules/Animal.ts --network localhost
Error HH303: Unrecognized task 'ignition'

For more info go to https://hardhat.org/HH303 or run Hardhat with --show-stack-traces

Screenshot 2024-03-27 at 17 45 19

jakubdonovan avatar Mar 27 '24 10:03 jakubdonovan

Yep, I've downgraded chai to 4.3.7 and everything works now

Stuck on the same thing, this worked for me!!!

zoosphar avatar Jun 08 '24 17:06 zoosphar

removing node_modules and using npm i instead of pnpm i also works.

hexcowboy avatar Jun 14 '24 13:06 hexcowboy

All of my previously working hardhat projects have become impossible to re-configure lately...

smitch88 avatar Jul 09 '24 17:07 smitch88