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

Convert worker to ESM #500

Merged
merged 6 commits into from
Sep 24, 2023
Merged

Convert worker to ESM #500

merged 6 commits into from
Sep 24, 2023

Conversation

ekzyis
Copy link
Member

@ekzyis ekzyis commented Sep 18, 2023

To use ESM for the worker, I created a package.json file in worker/ with { type: "module" } as its sole content.

I then rewrote every import to use ESM syntax.

I also tried to set { type: "module" } in the root package.json file to also use ESM in next.config.js.

However, this resulted in a weird problem: default imports were now getting imported as objects in this shape: { default: <defaultImport> }.

Afaik, this should only be the case if you use "import * as foo from 'bar'" syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#default_import

This is fixed by not using { type: "module" } for some reason. However, then, next.config.js also doesn't support ESM import syntax anymore.

The documentation says that if you want to use ESM, you can use next.config.mjs: https://nextjs.org/docs/pages/api-reference/next-config-js

But I didn't want to use MJS extension since I don't have any experience with it. For example, not sure if it's good style to mix JS with MJS etc. So I kept the CJS import syntax there.

@ekzyis
Copy link
Member Author

ekzyis commented Sep 18, 2023

Putting in draft to fix linter errors:

Run npm run lint

[email protected] lint
standard

standard: Use JavaScript Standard Style (https://standardjs.com/)
/home/runner/work/stacker.news/stacker.news/worker/auction.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/auction.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/earn.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/earn.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/index.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/index.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/nostr.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/nostr.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/ots.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/ots.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/repin.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/repin.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/search.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/search.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/streak.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/streak.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/trust.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/trust.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/views.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/views.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/wallet.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/wallet.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
Error: Process completed with exit code 1.

@ekzyis ekzyis marked this pull request as draft September 18, 2023 14:54
@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

Commit message from 98e899c

I wasn't able to fix the following errors:

/home/runner/work/stacker.news/stacker.news/worker/auction.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/auction.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/earn.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/earn.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/index.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/index.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/nostr.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/nostr.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/ots.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/ots.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/repin.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/repin.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/search.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/search.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/streak.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/streak.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/trust.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/trust.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/views.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/views.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/wallet.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/wallet.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)

I tried to tell babel where to find the babel config file (.babelrc), specifying the babel config in worker/package.json under "babel", using babel.config.json etc. to no avail.

However, afaict, we don't need babel for the worker since it won't run in a browser. Babel is only used to transpile code to target browsers.

But it still would be nice to lint the worker code with standard.

But we can figure this out later.

@ekzyis ekzyis marked this pull request as ready for review September 19, 2023 14:02
@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

One downside of using { "type": "module" } (and thus explicitly using ESM even though we were already able to use ESM import syntax 🤔) is that we have to provide the JS extension during relative (or absolute) imports now ... kind of annoying

@huumn
Copy link
Member

huumn commented Sep 19, 2023

That's weird. How does Next not have to do that? Some kind of webpack thing?

@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

That's weird. How does Next not have to do that? Some kind of webpack thing?

I'll check. Maybe we can adopt a similar solution.

Currently, the worker at least is not able to import stuff without the .js extension:

worker  | node:internal/errors:496
worker  |     ErrorCaptureStackTrace(err);
worker  |     ^
worker  |
worker  | Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/app/worker/wallet' imported from /app/worker/index.js
worker  |     at new NodeError (node:internal/errors:405:5)
worker  |     at finalizeResolution (node:internal/modules/esm/resolve:324:11)
worker  |     at moduleResolve (node:internal/modules/esm/resolve:943:10)
worker  |     at defaultResolve (node:internal/modules/esm/resolve:1129:11)
worker  |     at nextResolve (node:internal/modules/esm/loader:163:28)
worker  |     at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
worker  |     at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
worker  |     at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:77:40)
worker  |     at link (node:internal/modules/esm/module_job:76:36) {
worker  |   code: 'ERR_MODULE_NOT_FOUND'
worker  | }
worker  |
worker  | Node.js v18.17.0
worker  |
worker exited with code 1

The next app runs fine without the extension

@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

Somewhat related discussion: nodejs/node#46006

But I just noticed the worker code doesn't run without type: "module" in the root package.json since it think its imports are CommonJS 😱 I must have missed this somehow while fixing the app, oof

worker  | file:///app/worker/wallet.js:3
worker  | import { datePivot } from '../lib/time.js'
worker  |          ^^^^^^^^^
worker  | SyntaxError: Named export 'datePivot' not found. The requested module '../lib/time.js' is a CommonJS module, which may not support all module.exports as named exports.
worker  | CommonJS modules can always be imported via the default export, for example using:
worker  |
worker  | import pkg from '../lib/time.js';
worker  | const { datePivot } = pkg;
worker  |
worker  |     at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
worker  |     at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
worker  |
worker  | Node.js v18.17.0
worker  |
worker exited with code 1

Putting this back into draft.

@ekzyis ekzyis marked this pull request as draft September 19, 2023 16:43
@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

That's weird. How does Next not have to do that? Some kind of webpack thing?

I found out that they use this by printing the default webpack config:

   webpack: (config, { isServer, dev }) => {
+    console.log(config.module.rules)
     if (isServer) {

{ test: /.m?js/, resolve: { fullySpecified: false } }

@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

Trying to make "type": "module" in root package.json work to fix the worker imports.

Finally found a ticket related to the problem I run into then: vercel/next.js#34412

But no solution there... afaict, it should be fixed but it still happens for me.

But I think the solution is related to importInterop

But when I use

diff --git a/.babelrc b/.babelrc
index e5ef908..fbe2a18 100644
--- a/.babelrc
+++ b/.babelrc
@@ -15,6 +15,12 @@
                     ]
                 }
             }
+        ],
+        [
+            "@babel/plugin-transform-modules-commonjs",
+            {
+                "importInterop": "babel"
+            }
         ]
     ]
 }
\ No newline at end of file

These errors stop:

app  | TypeError: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20)
app  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
app  | LND GRPC connection successful
app  | - error pages/api/auth/[...nextauth].js (139:2) @ CredentialsProvider
app  | - error Error [TypeError]: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20) {
app  |   digest: undefined
app  | }
app  |   137 |
app  |   138 | const providers = [
app  | > 139 |   CredentialsProvider({
app  |       |  ^
app  |   140 |     id: 'lightning',
app  |   141 |     name: 'Lightning',
app  |   142 |     credentials: {
app  | TypeError: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20)
app  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

but I get new errors now:

app  | Error: Cannot find module '../styles/globals.scss'

🙄

Then I tried to use a function for importInterop to exclude scss imports so I need to use babel.config.js:

app  | Error [ERR_REQUIRE_ESM]: require() of ES Module /app/babel.config.js from /app/node_modules/next/dist/build/babel/loader/get-config.js not supported.
app  | babel.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
app  | Instead rename babel.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /app/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

So I used babel.config.cjs:

app  | - info Using external babel configuration from /app/babel.config.cjs
app  | - error ./sw/index.js
app  | Error: The Next.js Babel loader does not support .mjs or .cjs config files.
app  | - wait compiling...
app  | - error ./sw/index.js
app  | Error: The Next.js Babel loader does not support .mjs or .cjs config files.
app  | - info Loaded env from /app/.env
app  | - info Loaded env from /app/.env
app  | - wait compiling /~ (client and server)...
app  | - error ./node_modules/next/dist/client/next-dev.js
app  | Error: The Next.js Babel loader does not support .mjs or .cjs config files.
app  | - wait compiling /[name] (client and server)...
app  | - error ./node_modules/next/dist/client/next-dev.js
app  | Error: The Next.js Babel loader does not support .mjs or .cjs config files.

...

(just trying to write down my process so I don't start to run in circles, lol)

UPDATE:

And using babel-plugin-transform-scss fixes

app  | Error: Cannot find module '../styles/globals.scss'

this but then I get hit by

app  | Error: Cannot find module '../components/me'

So I think everything is broken now, lol

@huumn
Copy link
Member

huumn commented Sep 19, 2023

the .js isn't worth it if that's what this is about

this is why it's been commonjs for so long. I don't tend to do configuration programming very well

@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

the .js isn't worth it if that's what this is about

No, it's not about this at the moment. I realized that there is a conflict between app and worker:

the app doesn't work with setting { "type": "module" } in package.json because then I get these weird default import error:

app  | TypeError: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20)
app  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
app  | LND GRPC connection successful
app  | - error pages/api/auth/[...nextauth].js (139:2) @ CredentialsProvider
app  | - error Error [TypeError]: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20) {
app  |   digest: undefined
app  | }
app  |   137 |
app  |   138 | const providers = [
app  | > 139 |   CredentialsProvider({
app  |       |  ^
app  |   140 |     id: 'lightning',
app  |   141 |     name: 'Lightning',
app  |   142 |     credentials: {
app  | TypeError: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20)
app  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

which I believe is related to importInterop

But it's required to allow imports from the worker since else, it assumes the imports to be from CommonJS:

worker  | file:///app/worker/wallet.js:3
worker  | import { datePivot } from '../lib/time.js'
worker  |          ^^^^^^^^^
worker  | SyntaxError: Named export 'datePivot' not found. The requested module '../lib/time.js' is a CommonJS module, which may not support all module.exports as named exports.
worker  | CommonJS modules can always be imported via the default export, for example using:
worker  |
worker  | import pkg from '../lib/time.js';
worker  | const { datePivot } = pkg;
worker  |
worker  |     at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
worker  |     at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
worker  |
worker  | Node.js v18.17.0
worker  |
worker exited with code 1

but I can just try to use what is suggested in the error instead of trying to make ESM imports work:

worker  | CommonJS modules can always be imported via the default export, for example using:
worker  |
worker  | import pkg from '../lib/time.js';
worker  | const { datePivot } = pkg;

this is why it's been commonjs for so long. I don't tend to do configuration programming very well

I guess it's more specifically related to Javascript configuration, haha

There are so many layers: babel, webpack, ESM, CJS and then NextJS on top which tries to handle all of them at the same time

@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

Nah, that also doesn't work ... the worker then doesn't understand import inside the files it wants to import stuff from:

worker  | (node:7) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
worker  | (Use `node --trace-warnings ...` to show where the warning was created)
worker  | /app/api/resolvers/serial.js:1
worker  | import { GraphQLError } from 'graphql'
worker  | ^^^^^^
worker  |
worker  | SyntaxError: Cannot use import statement outside a module
worker  |     at internalCompileFunction (node:internal/vm:73:18)
worker  |     at wrapSafe (node:internal/modules/cjs/loader:1178:20)
worker  |     at Module._compile (node:internal/modules/cjs/loader:1220:27)
worker  |     at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
worker  |     at Module.load (node:internal/modules/cjs/loader:1119:32)
worker  |     at Module._load (node:internal/modules/cjs/loader:960:12)
worker  |     at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29)
worker  |     at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
worker  |
worker  | Node.js v18.17.0
worker  |
worker exited with code 1

So we're back to square one. Can't import ESM stuff from the app in the worker without using { "type": "module" } in the root package.json. That's why we've been converting the stuff that the worker also uses to CJS.

But then this CJS creeps more and more into the app codebase, which I wanted to finally avoid

UPDATE: Oh, and I wanted to import gfm stuff like gfmFromMarkdown in the worker to run imgproxy stuff in the worker which requires to convert the worker to ESM:

This package is ESM only. In Node.js (version 16+), install with npm:

npm install mdast-util-gfm

-- https://www.npmjs.com/package/mdast-util-gfm#install

@ekzyis
Copy link
Member Author

ekzyis commented Sep 19, 2023

Okay, I just created a package.json with { "type": "module" } in lib/ and api/ in 5c3e428 to tell the worker that these should be handled as MJS files.

This doesn't break the app but fixes the worker.

I also noticed that we used global in lib/apollo.js in a browser context. Not sure how this worked before - was this code not executed before on the browser? Replaced that with window in 3c89602

@ekzyis ekzyis marked this pull request as ready for review September 20, 2023 15:40
To use ESM for the worker, I created a package.json file in worker/ with `{ type: "module" }` as its sole content.

I then rewrote every import to use ESM syntax.

I also tried to set `{ type: "module" }` in the root package.json file to also use ESM in next.config.js.

However, this resulted in a weird problem: default imports were now getting imported as objects in this shape: `{ default: <defaultImport> }`.

Afaik, this should only be the case if you use "import * as foo from 'bar'" syntax: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#default_import

This is fixed by not using `{ type: "module" }` for some reason. However, then, next.config.js also doesn't support ESM import syntax anymore.

The documentation says that if you want to use ESM, you can use next.config.mjs: https://nextjs.org/docs/pages/api-reference/next-config-js

But I didn't want to use MJS extension since I don't have any experience with it. For example, not sure if it's good style to mix JS with MJS etc. So I kept the CJS import syntax there.
I wasn't able to fix the following errors:

/home/runner/work/stacker.news/stacker.news/worker/auction.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/auction.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/earn.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/earn.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/index.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/index.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/nostr.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/nostr.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/ots.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/ots.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/repin.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/repin.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/search.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/search.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/streak.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/streak.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/trust.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/trust.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/views.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/views.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)
/home/runner/work/stacker.news/stacker.news/worker/wallet.js:0:0: Parsing error: No Babel config file detected for /home/runner/work/stacker.news/stacker.news/worker/wallet.js. Either disable config file checking with requireConfigFile: false, or configure Babel so that it can find the config files. (null)

I tried to tell babel where to find the babel config file (.babelrc), specifying the babel config in worker/package.json under "babel", using babel.config.json etc. to no avail.

However, afaict, we don't need babel for the worker since it won't run in a browser. Babel is only used to transpile code to target browsers.

But it still would be nice to lint the worker code with standard.

But we can figure this out later.
This fixes the issue that we can't use `{ "type": "module" }` in the root package.json since it breaks the app with this error:

  app  | TypeError: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
  app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20)
  app  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  app  | LND GRPC connection successful
  app  | - error pages/api/auth/[...nextauth].js (139:2) @ CredentialsProvider
  app  | - error Error [TypeError]: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
  app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20) {
  app  |   digest: undefined
  app  | }
  app  |   137 |
  app  |   138 | const providers = [
  app  | > 139 |   CredentialsProvider({
  app  |       |  ^
  app  |   140 |     id: 'lightning',
  app  |   141 |     name: 'Lightning',
  app  |   142 |     credentials: {
  app  | TypeError: next_auth_providers_credentials__WEBPACK_IMPORTED_MODULE_2__ is not a function
  app  |     at eval (webpack-internal:///./pages/api/auth/[...nextauth].js:218:20)
  app  |     at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

build but we need to tell the worker that the files are MJS, else we get this error:

  worker  | file:///app/worker/wallet.js:3
  worker  | import { datePivot } from '../lib/time.js'
  worker  |          ^^^^^^^^^
  worker  | SyntaxError: Named export 'datePivot' not found. The requested module '../lib/time.js' is a CommonJS module, which may not support all module.exports as named exports.
  worker  | CommonJS modules can always be imported via the default export, for example using:
  worker  |
  worker  | import pkg from '../lib/time.js';
  worker  | const { datePivot } = pkg;
  worker  |
  worker  |     at ModuleJob._instantiate (node:internal/modules/esm/module_job:124:21)
  worker  |     at async ModuleJob.run (node:internal/modules/esm/module_job:190:5)
  worker  |
  worker  | Node.js v18.17.0
  worker  |
  worker exited with code 1
I did not want to do this but I was not able to fix this error in any other way I tried:

  /home/ekzyis/programming/stacker.news/api/lnd/index.js:0:0: Parsing error: No Babel config file detected for /home/ekzyis/programming/stacker.news/api/lnd/index.js. Either disable config file checking with requ
ireConfigFile: false, or configure Babel so that it can find the config files. (null)

Did not want to look deeper into all this standard, eslint, babel configuration stuff ...
@ekzyis ekzyis mentioned this pull request Sep 21, 2023
12 tasks
@huumn huumn merged commit dde82e2 into stackernews:master Sep 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants