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

ESM import error #16

Open
staeke opened this issue Mar 14, 2023 · 21 comments
Open

ESM import error #16

staeke opened this issue Mar 14, 2023 · 21 comments
Labels
question Further information is requested

Comments

@staeke
Copy link

staeke commented Mar 14, 2023

Running one of the below

// dev.mjs
import { watch } from 'turbowatch';

// OR dev.cjs
const { watch } = require('turbowatch');

produces

const zx_1 = require("zx");

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/staffan/dev/lr2/node_modules/zx/build/index.js from /Users/staffan/dev/lr2/node_modules/turbowatch/dist/createSpawn.js not supported.
...

Expected Behavior

It should work to import the library

Possible Solution

Publish turbowatch as ESM. Possibly you could consider hybrid, but it seems zx is ESM only (due to node-fetch?), as well as serialize-error

Or am I missing something?

@staeke staeke added the bug Something isn't working label Mar 14, 2023
@gajus
Copy link
Owner

gajus commented Mar 15, 2023

Can you try running turbowatch ./dev.mjs?

@gajus gajus added question Further information is requested and removed bug Something isn't working labels Mar 15, 2023
@gajus
Copy link
Owner

gajus commented Mar 15, 2023

Closing as there is no answer and I cannot replicate this.

@gajus gajus closed this as completed Mar 15, 2023
@staeke
Copy link
Author

staeke commented Mar 15, 2023

You didn't give me a lot of time there :)

Anyhow, that does indeed work. As does renaming .mjs to .ts and running node -r jiti/register dev.ts. Maybe make that clearer in docs?

@NickHeiner
Copy link

I'm running into this same issue, but I'm not sure what the fix is. What's ./dev.mjs?

@gajus
Copy link
Owner

gajus commented Apr 7, 2023

@NickHeiner Just do turbowatch your-script.ts

@NickHeiner
Copy link

I'm using turbowatch programmatically.

My goal is to detect when files in a directory change, then take action via the event emitter.

@gajus
Copy link
Owner

gajus commented Apr 8, 2023

@NickHeiner In that case, you will need to either adopt esm or use something like node --loader tsx your-script.ts

@virtuallyunknown
Copy link

@NickHeiner In that case, you will need to either adopt esm or use something like node --loader tsx your-script.ts

Hey @gajus, do you mind taking a look at this bare minimum repo where the error can be reproduced?

There is only a single package.json script called dev. Running it would produce this error:

> node --loader tsx turbowatch.ts

(node:179171) ExperimentalWarning: Custom ESM Loaders is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
/home/user/projects/turbowatch-error/node_modules/turbowatch/dist/createSpawn.js:15
const zx_1 = require("zx");
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/projects/turbowatch-error/node_modules/zx/build/index.js from /home/user/projects/turbowatch-error/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in /home/user/projects/turbowatch-error/node_modules/turbowatch/dist/createSpawn.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/home/user/projects/turbowatch-error/node_modules/turbowatch/dist/createSpawn.js:15:14)
    at Object.<anonymous> (/home/user/projects/turbowatch-error/node_modules/turbowatch/dist/subscribe.js:4:23)
    at Object.<anonymous> (/home/user/projects/turbowatch-error/node_modules/turbowatch/dist/watch.js:8:21)
    at Object.<anonymous> (/home/user/projects/turbowatch-error/node_modules/turbowatch/dist/index.js:14:15) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v20.3.1

Not exactly sure what is causing this, a week or so ago everything was working, but unfortunately I am still early in development and wasn't keeping history.

As you can see I'm using Node 20 with package.json of type: module. Any idea what might be causing this or a tip how to fix it?

Cheers!

@gajus gajus reopened this Jun 23, 2023
@RobM-ADP
Copy link

I'm seeing the same issue as @virtuallyunknown just by copying the example from the docs. Same issue with node 18 as well. Is there any example somewhere of it working?

@gajus
Copy link
Owner

gajus commented Jul 20, 2023

I'm seeing the same issue as @virtuallyunknown just by copying the example from the docs. Same issue with node 18 as well. Is there any example somewhere of it working?

#16 (comment)

It is not an issue if you are using turbowatch binary.

@virtuallyunknown
Copy link

I was previously using node --loader tsx turbowatch.ts because for some reason when you use the turbowatch binary you have to ctrl+c twice to terminate the process, the first time it does nothing, only terminates after the second time.

This might sound like a very silly thing to complain about, but when you're developing and restarting your monorepo all day long it becomes annoying.

@jyothiprakash-frontend
Copy link

jyothiprakash-frontend commented Aug 3, 2023

I am also facing same issue like above.

Build triggering for following services
platform
step1

kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js:15
const zx_1 = require("zx");
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/kf-xg-frontend/node_modules/.pnpm/zx@7.2.3/node_modules/zx/build/index.js from /Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in /Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js:15:14)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/subscribe.js:4:23)
    at Object.<anonymous> (/Userskf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/watch.js:8:21)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/index.js:14:15)
    at async Promise.all (index 0)
    at async watchService (/Users//kf-xg-frontend/build/run.js:184:3) {
  code: 'ERR_REQUIRE_ESM'
}

@gajus
Copy link
Owner

gajus commented Aug 3, 2023

I was previously using node --loader tsx turbowatch.ts because for some reason when you use the turbowatch binary you have to ctrl+c twice to terminate the process, the first time it does nothing, only terminates after the second time.

That's a graceful termination feature.

The first time you send a signal with ctrl+c it initiates graceful termination and waits for all services to shutdown.

If you don't wait and press again, then you force terminate all scripts.

@virtuallyunknown
Copy link

The first time you send a signal with ctrl+c it initiates graceful termination and waits for all services to shutdown.

If you don't wait and press again, then you force terminate all scripts.

Any way to disable this? I am only using turbowatch in development, data loss and whatever else graceful termination prevents is not a risk for me.

@gajus
Copy link
Owner

gajus commented Aug 3, 2023

Any way to disable this? I am only using turbowatch in development, data loss and whatever else graceful termination prevents is not a risk for me.

I cannot imagine a scenario where this is desirable. You will be leaving hanging services left and right.

If you want your services to shutdown immediately, then add the logic to the application itself to shutdown immediately (when in NODE_ENV=development).

@virtuallyunknown
Copy link

You said that the first signal is graceful termination, and the second signal is forceful termination. So I was asking if there is a way I can instead configure the first signal to be the forceful termination.

@AlexAegis
Copy link

I had the same

const zx_1 = require("zx");
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module

error earlier too, I made a package to host my turbowatch configuration, originally I had the watch fn call inside it and that resulted in this error. (I'm using "type": "module", vite for bundling)
I removed all actual imports from turbowatch and kept only type imports, and now it's working.

The package for reference (It's very far from a lightweight reproduction, I didn't mean it as such):
https://github.com/AlexAegis/js-tooling/blob/34f65bf2332d4524f81c559848c8f3a9f9071cb2/packages/turbowatch/src/internal/watch-local-node-modules.ts

On a sidenote, I have a very nice and functional setup with the above configuration, thank you very much for this project!

@gajus
Copy link
Owner

gajus commented Aug 3, 2023

You said that the first signal is graceful termination, and the second signal is forceful termination. So I was asking if there is a way I can instead configure the first signal to be the forceful termination.

You could, but depending on what services you are running, there is no guarantee that it will shutdown all the services. I would not do it as a default as it is bound to cause more confusion than anything.

@virtuallyunknown
Copy link

You could, but depending on what services you are running, there is no guarantee that it will shutdown all the services. I would not do it as a default as it is bound to cause more confusion than anything.

I am absolutely not asking you to make this the default behavior of turbowatch, that would be quite irresponsible of me since I don't know what complications would arise from that. I was merely asking if you could tell me how I can configure this for my own project.

@jyothiprakash-frontend
Copy link

I am also facing same issue like above.

Build triggering for following services platform step1

kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js:15
const zx_1 = require("zx");
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/kf-xg-frontend/node_modules/.pnpm/zx@7.2.3/node_modules/zx/build/index.js from /Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in /Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js:15:14)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/subscribe.js:4:23)
    at Object.<anonymous> (/Userskf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/watch.js:8:21)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/index.js:14:15)
    at async Promise.all (index 0)
    at async watchService (/Users//kf-xg-frontend/build/run.js:184:3) {
  code: 'ERR_REQUIRE_ESM'
}

Can Some please help me on this.Thanks in advance.

@3rd
Copy link

3rd commented Dec 28, 2023

Just hit this as well when trying to use turbowatch as a library in an ESM project.

nsa/silly-goose/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js:15
const zx_1 = require("zx");
             ^

Error [ERR_REQUIRE_ESM]: require() of ES Module nsa/silly-goose/node_modules/.pnpm/zx@7.2.3/node_modules/zx/build/index.js from nsa/silly-goose/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in nsa/silly-goose/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (nsa/silly-goose/node_modules/.pnpm/turbowatch@2.29.4/node_modules/turbowatch/dist/createSpawn.js:15:14) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

8 participants