Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[turborepo] Codegenerator not support ESM #5657

Closed
peam1146 opened this issue Aug 2, 2023 · 11 comments · Fixed by #8109
Closed

[turborepo] Codegenerator not support ESM #5657

peam1146 opened this issue Aug 2, 2023 · 11 comments · Fixed by #8109
Assignees

Comments

@peam1146
Copy link

peam1146 commented Aug 2, 2023

What version of Turborepo are you using?

v1.10.9

What package manager are you using / does the bug impact?

Yarn v2/v3 (node_modules linker only)

What operating system are you using?

Mac

Describe the Bug

Unexpected error. Please report it as a bug:
Must use import to load ES Module: /Users/peam/work/softnetics/<app>/turbo/generators/config.ts
require() of ES modules is not supported.
require() of /Users/peam/work/softnetics/<app>/turbo/generators/config.ts from /Users/peam/.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/peam/work/softnetics/<app>/package.json.

I got this error when use "type: 'module'" in package.json

Expected Behavior

It should work ...

To Reproduce

use "type: 'module'" in package.json

Reproduction Repo

No response

@peam1146 peam1146 added area: turborepo kind: bug Something isn't working needs: triage New issues get this label. Remove it after triage labels Aug 2, 2023
@peam1146
Copy link
Author

peam1146 commented Aug 2, 2023

It works when I use .cjs with jsDoc instead

@tknickman
Copy link
Member

This is a known limitation at the moment, we're working to provide full support for ESM dependencies in the future!

@tknickman tknickman added story and removed kind: bug Something isn't working needs: triage New issues get this label. Remove it after triage labels Aug 4, 2023
@allejo
Copy link

allejo commented Dec 7, 2023

It works when I use .cjs with jsDoc instead

Simply to expand on what @peam1146 mentioned with an example.

Go from this,

import type { PlopTypes } from '@turbo/gen';

export default function generator(plop: PlopTypes.NodePlopAPI): void {
  // ...
}

to this,

/** @param plop {import('@turbo/gen').PlopTypes.NodePlopAPI} */
module.exports = (plop) => {
   // ...
};

And everyone's happy.

@Nilegfx
Copy link

Nilegfx commented Dec 27, 2023

I think this is an important feature as it blocks us from having code generation in type: module packages. even if we tried the suggested workaround in @allejo comment above, we still have to change all import statements down the flow to CJS imports too. this is very annoying issue.

@wesleycoder
Copy link
Contributor

I was digging into this issue and found out that adding a package.json with "type": "commonjs" to turbo/generators is enough to resolve this issue, so I filed the PR above.

While this is not merged if anyone stumbles into this you can create the file by hand, including on already existent templates without the need to convert the config.ts or config.js to commonjs format.

#typesafety_ftw

tknickman pushed a commit that referenced this issue May 8, 2024
### Description

As described by #5657, the default `simple-*` templates creates a
template that is broken for `"type": "module"` packages.

When digging into this issue I found out that a simple `package.json`
with `"type": "commonjs"` solves the problems by telling the runtime to
treat the file diferently from the parent package.

### Testing Instructions

- Initialize a new monorepo with `"type": "module"` in the
`package.json`
- on this branch it should now be possible to generate the sample
generator (either `js` or `ts`) and not have any errors about the module
type.
- it should also be possible to run the generator and it should create
the file in the project root normally.
resolves #5657
Neosoulink pushed a commit to Neosoulink/turbo that referenced this issue Jun 14, 2024
### Description

As described by vercel#5657, the default `simple-*` templates creates a
template that is broken for `"type": "module"` packages.

When digging into this issue I found out that a simple `package.json`
with `"type": "commonjs"` solves the problems by telling the runtime to
treat the file diferently from the parent package.

### Testing Instructions

- Initialize a new monorepo with `"type": "module"` in the
`package.json`
- on this branch it should now be possible to generate the sample
generator (either `js` or `ts`) and not have any errors about the module
type.
- it should also be possible to run the generator and it should create
the file in the project root normally.
resolves vercel#5657
@umar-ahmed
Copy link

@tknickman @wesleycoder While this works to let you run the generator file, it doesn't fix the original issue that generators don't support ESM.

So if I import an ESM package into one of my generators, it will still complain about require() of ESM module. I think we should re-open this issue OR I can file a new issue if that's preferable

@paolostyle
Copy link

Why is this issue closed? My entire codebase is in ESM format and this silly workaround with package.json is not a solution. At the very least this should work with a config.cts/config.cjs file.

@wesleycoder
Copy link
Contributor

AFAIK the support for ESM should come from the underlying plopjs/plop package and turborepo has no way of supporting this on it's own.
Consider opening an issue there if this continues to impede your use-case/workflow.

@paolostyle
Copy link

paolostyle commented Oct 9, 2024

I ended up using plop directly. Turborepo is using version 0.26.0 of node-plop (latest is 0.32.0, due to semver it's stuck at 0.26.0 despite the ^) which I presume does not support ESM, but the latest versions of plop packages are ESM-based. I tried overriding the version of node-plop through pnpm.overrides but that didn't help either. I don't see much value in using Turbo's wrapper anyway.

@umar-ahmed
Copy link

latest is 0.32.0, due to semver it's stuck at 0.26.0 despite the ^

@paolostyle Caret behaves differently for 0.x.y version ranges. It will never upgrade past 0.26.x

@wesleycoder are there any plans to upgrade the version of Plop within Turborepo? I think it might resolve the issues that people like me are having with the way turbo gen handles ESM files 🙏

@paolostyle
Copy link

paolostyle commented Oct 9, 2024

I know how it behaves, that's what I meant by "stuck" and "due to semver", but I suppose I didn't express my thoughts properly, sorry about that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

9 participants