gajus / turbowatch

Extremely fast file change detector and task orchestrator for Node.js.
Other
944 stars 23 forks source link

ESM import error #16

Open staeke opened 1 year ago

staeke commented 1 year ago

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?

gajus commented 1 year ago

Can you try running turbowatch ./dev.mjs?

gajus commented 1 year ago

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

staeke commented 1 year ago

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 commented 1 year ago

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

gajus commented 1 year ago

@NickHeiner Just do turbowatch your-script.ts

NickHeiner commented 1 year ago

I'm using turbowatch programmatically.

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

gajus commented 1 year ago

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

virtuallyunknown commented 1 year ago

@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!

RobM-ADP commented 1 year ago

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 commented 1 year ago

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?

https://github.com/gajus/turbowatch/issues/16#issuecomment-1469052823

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

virtuallyunknown commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 11 months ago

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