woocommerce/woocommerce-admin

create-wc-extension ES Module Errors

iamleese opened this issue · 1 comments

Describe the bug

When running create-wc-extension, ERR_REQUIRE_ESM Error is thrown

To Reproduce

npm run create-wc-extension

Actual behavior:

The Following Error is thrown:
Error [ERR_REQUIRE_ESM]: require() of ES Module {wp_directory_path}/plugins/woocommerce-admin/node_modules/.pnpm/chalk@5.0.1/node_modules/chalk/source/index.js from {wp_directory_path}/plugins/woocommerce-admin/bin/starter-pack/starter-pack.js not supported.
Instead change the require of index.js in {wp_directory_path}/plugins/woocommerce-admin/bin/starter-pack/starter-pack.js to a dynamic import() which is available in all CommonJS modules.
at Object. (/ {wp_directory_path}/plugins/woocommerce-admin/bin/starter-pack/starter-pack.js:4:15) {
code: 'ERR_REQUIRE_ESM'
}

Expected behavior

Welcome message and extension instructions should appear.

My Solution

In the package.json, add
"type": "module"

In /bin/starter-pack/starter-pack.js, use import instead of require() and define the __filename and __dirname constants.

//const fs = require( 'fs-extra' );
//const path = require( 'path' );
//const promptly = require( 'promptly' );
//const chalk = require( 'chalk' );

import fs from 'fs-extra';
import path from 'path';
import promptly from 'promptly';
import chalk from 'chalk';
import {fileURLToPath} from 'url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

Hey @iamleese Thanks for reporting this! Actually, we have merged this repo to woocommerce/woocommerce recently and we have also fixed this bug here. Could you clone woocommerce and run the following commands to see if it works for you:

pnpm install
pnpm nx create-wc-extension woocommerce-admin

If you're still facing the issue, please free feel to file an issue there. I'm going to close this one. Thank you!