Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported when trying to run forge deploy from a package with
"type": "module". Is there any way to fix this?
You could install the Forge CLI in a global namespace rather than as a dependency of your module (or in a separate directory and specify the path when calling it).
Can you please show a minimal
package.json that reproduces the problem?
Easy reproduction steps.
- Create a new directory
- Init the package (yarn init or npm init)
"type": "module"in package.json
- Install any module that is used by @forge/cli internally, in this case
chalk(yarn add chalk, npm install chalk).
- Call the forge cli with
yarn forge deployor
npx forge deploy.
Geting a require ESM error. You can find a screenshot attached of the minimal reproduction.
This is probably a result of a shared dependency of Forge and your app being installed as an ES module version. Forge CLI isn’t an ES project at the moment, and the installation guide recommends installing it globally so as not to share
node_modules with other projects.
If you want to develop an ES module project, you can either install Forge CLI globally, or create its own project and run it from there using the full path.
I understand, are there any plans to update
@forge/cli to be an ESM project?
Not at the moment, sorry. Specifically
@forge/cli is not designed as a package (to be imported), and other packages (like
@forge/api) will then be more difficult to import from CommonJS.
Untested suggestion: you might want to investigate how do different package managers (Yarn or NPM) deduplicate the dependencies of different ESM/CommonJS packages. Perhaps you will have more success with one or the other, or they (package managers) will respond to bug reports.