plopjs / plop

Consistency Made Simple
http://plopjs.com
MIT License
7.12k stars 277 forks source link

[node-plop] Support ESM configs #387

Closed darvids0n closed 1 year ago

darvids0n commented 1 year ago

Hi! Thanks for writing Plop, it's been super helpful for automating developer processes at my workplace.

Until recently I'd been using node-plop on projects which were treated as interop/CommonJS (no type=module in package.json), though the implementation itself is still ESM. However, due to my build config being ESM and getting linting issues I have started specifying the type field.

Specifying type: "module" causes plop to fail to load any configs, as node-plop has been transpiled to cjs in the distribution, which turns the await import of the plopfile into a require only valid for CJS files.

Has there been any decision made around ESM, eg bundling an ESM distribution alongside CJS? It would be good to have it.

Here's a representative stack trace (via Turborepo):

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/darvids0n/Projects/sample-app/turbo/generators/config.ts
require() of ES modules is not supported.
require() of /Users/darvids0n/Projects/sample-app/turbo/generators/config.ts from /Users/darvids0n/.npm/_npx/111d46748c4e70c7/node_modules/node-plop/lib/node-plop.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /Users/darvids0n/Projects/sample-app/package.json.

    at createErrRequireEsm (/Users/darvids0n/.npm/_npx/111d46748c4e70c7/node_modules/ts-node/dist-raw/node-internal-errors.js:46:15)
    at assertScriptCanLoadAsCJSImpl (/Users/darvids0n/.npm/_npx/111d46748c4e70c7/node_modules/ts-node/dist-raw/node-internal-modules-cjs-loader.js:584:11)
    at Object.require.extensions.<computed> [as .ts] (/Users/darvids0n/.npm/_npx/111d46748c4e70c7/node_modules/ts-node/src/index.ts:1610:5)
    at Module.load (node:internal/modules/cjs/loader:1117:32)
    at Function.Module._load (node:internal/modules/cjs/loader:958:12)
    at Module.require (node:internal/modules/cjs/loader:1141:19)
    at require (node:internal/modules/cjs/helpers:110:18)
    at nodePlop (/Users/darvids0n/.npm/_npx/111d46748c4e70c7/node_modules/node-plop/lib/node-plop.js:297:28)
rashinp commented 1 year ago

I am trying to use node-plop on a node js project, but unfortunately get the same error while trying to import node-plop.

crutchcorn commented 1 year ago

This is strange, as Plop 3 is an ESM package and prefers ESM packages. Under-the-hood, we're using import to load ESM files.

Which version of plop/node-plop are you using and can you provide a minimal repro?

darvids0n commented 1 year ago

The version of node-plop pinned in @turbo/gen is the one I'm using - currently 0.26.3.

Here's a stackblitz of the issue.

crutchcorn commented 1 year ago

That'd be why. node-plop introduced ESM in 0.30, you'll have to ask downstream to upgrade their instance of the lib.

Happy to help them upgrade the package if they reach out with any challenges or questions :)

As this is an issue downstream, I'll close this issue