turbowatch icon indicating copy to clipboard operation
turbowatch copied to clipboard

ESM import error

Open staeke opened this issue 2 years ago • 21 comments
trafficstars

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 avatar Mar 14 '23 20:03 staeke

Can you try running turbowatch ./dev.mjs?

gajus avatar Mar 15 '23 00:03 gajus

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

gajus avatar Mar 15 '23 16:03 gajus

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?

staeke avatar Mar 15 '23 22:03 staeke

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

NickHeiner avatar Apr 06 '23 22:04 NickHeiner

@NickHeiner Just do turbowatch your-script.ts

gajus avatar Apr 07 '23 00:04 gajus

I'm using turbowatch programmatically.

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

NickHeiner avatar Apr 07 '23 00:04 NickHeiner

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

gajus avatar Apr 08 '23 16:04 gajus

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

virtuallyunknown avatar Jun 23 '23 04:06 virtuallyunknown

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?

RobM-ADP avatar Jul 20 '23 15:07 RobM-ADP

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.

gajus avatar Jul 20 '23 22:07 gajus

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.

virtuallyunknown avatar Jul 20 '23 23:07 virtuallyunknown

I am also facing same issue like above.

Build triggering for following services platform step1

kf-xg-frontend/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/zx/build/index.js from /Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in /Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/turbowatch/dist/createSpawn.js:15:14)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/subscribe.js:4:23)
    at Object.<anonymous> (/Userskf-xg-frontend/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/watch.js:8:21)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/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'
}

jyothiprakash-frontend avatar Aug 03 '23 10:08 jyothiprakash-frontend

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.

gajus avatar Aug 03 '23 14:08 gajus

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.

virtuallyunknown avatar Aug 03 '23 16:08 virtuallyunknown

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).

gajus avatar Aug 03 '23 16:08 gajus

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.

virtuallyunknown avatar Aug 03 '23 17:08 virtuallyunknown

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!

AlexAegis avatar Aug 03 '23 18:08 AlexAegis

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.

gajus avatar Aug 03 '23 21:08 gajus

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.

virtuallyunknown avatar Aug 03 '23 22:08 virtuallyunknown

I am also facing same issue like above.

Build triggering for following services platform step1

kf-xg-frontend/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/zx/build/index.js from /Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in /Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/turbowatch/dist/createSpawn.js:15:14)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/subscribe.js:4:23)
    at Object.<anonymous> (/Userskf-xg-frontend/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/watch.js:8:21)
    at Object.<anonymous> (/Users/kf-xg-frontend/node_modules/.pnpm/[email protected]/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.

jyothiprakash-frontend avatar Aug 04 '23 04:08 jyothiprakash-frontend

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

nsa/silly-goose/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/zx/build/index.js from nsa/silly-goose/node_modules/.pnpm/[email protected]/node_modules/turbowatch/dist/createSpawn.js not supported.
Instead change the require of index.js in nsa/silly-goose/node_modules/.pnpm/[email protected]/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/[email protected]/node_modules/turbowatch/dist/createSpawn.js:15:14) {
  code: 'ERR_REQUIRE_ESM'
}

Node.js v21.4.0

3rd avatar Dec 28 '23 23:12 3rd