terascope / teraslice

Scalable data processing pipelines in JavaScript
https://terascope.github.io/teraslice/
Apache License 2.0
50 stars 13 forks source link

Node 22 'Import assertions' no longer allowed #3622

Closed sotojn closed 1 month ago

sotojn commented 2 months ago

When trying to build a docker image using node 22.2.0, I come across this error:

MacBook-Pro teraslice % docker compose up -d --build
[+] Building 53.6s (18/28)                                                                                                                                                                                                                       docker:desktop-linux
 => [teraslice-worker internal] load build definition from Dockerfile                                                                                                                                                                                            0.0s
 => => transferring dockerfile: 1.16kB                                                                                                                                                                                                                           0.0s
 => [teraslice-master internal] load build definition from Dockerfile                                                                                                                                                                                            0.0s
 => => transferring dockerfile: 1.16kB                                                                                                                                                                                                                           0.0s
 => [teraslice-worker internal] load metadata for docker.io/terascope/node-base:22.2.0                                                                                                                                                                           0.7s
 => [teraslice-master internal] load .dockerignore                                                                                                                                                                                                               0.0s
 => => transferring context: 336B                                                                                                                                                                                                                                0.0s
 => [teraslice-worker internal] load .dockerignore                                                                                                                                                                                                               0.0s
 => => transferring context: 336B                                                                                                                                                                                                                                0.0s
 => [teraslice-master internal] load build context                                                                                                                                                                                                               0.1s
 => => transferring context: 104.34kB                                                                                                                                                                                                                            0.1s
 => [teraslice-worker  1/11] FROM docker.io/terascope/node-base:22.2.0@sha256:071e1ca28a232d6c18cff1157df5b164f2705f95624ad6df7db5d48e5b645120                                                                                                                   0.0s
 => [teraslice-worker internal] load build context                                                                                                                                                                                                               0.1s
 => => transferring context: 104.34kB                                                                                                                                                                                                                            0.1s
 => CACHED [teraslice-master  2/11] COPY package.json yarn.lock tsconfig.json .yarnrc /app/source/                                                                                                                                                               0.0s
 => CACHED [teraslice-master  3/11] COPY .yarnclean.ci /app/source/.yarnclean                                                                                                                                                                                    0.0s
 => CACHED [teraslice-master  4/11] COPY .yarn /app/source/.yarn                                                                                                                                                                                                 0.0s
 => [teraslice-master  5/11] COPY packages /app/source/packages                                                                                                                                                                                                  0.2s
 => [teraslice-master  6/11] COPY scripts /app/source/scripts                                                                                                                                                                                                    0.0s
 => [teraslice-master  7/11] COPY types /app/source/types                                                                                                                                                                                                        0.0s
 => [teraslice-master  8/11] RUN yarn --prod=false --frozen-lockfile     && yarn build     && yarn       --prod=true       --silent       --frozen-lockfile       --skip-integrity-check       --ignore-scripts     && yarn cache clean                         51.9s
 => [teraslice-master  9/11] COPY service.js /app/source/                                                                                                                                                                                                        0.0s 
 => [teraslice-master 10/11] RUN node -e "require('node-rdkafka')"                                                                                                                                                                                               0.2s 
 => ERROR [teraslice-master 11/11] RUN node -e "import('teraslice')"                                                                                                                                                                                             0.3s 
------                                                                                                                                                                                                                                                                
 > [teraslice-master 11/11] RUN node -e "import('teraslice')":                                                                                                                                                                                                        
0.309 file:///app/source/packages/teraslice/dist/src/lib/storage/backends/elasticsearch_store.js:8                                                                                                                                                                    
0.309 import analyticsSchema from './mappings/analytics.json' assert { type: 'json' };                                                                                                                                                                                
0.309                                                         ^^^^^^                                                                                                                                                                                                  
0.309                                                                                                                                                                                                                                                                 
0.309 SyntaxError: Unexpected identifier 'assert'
0.309     at compileSourceTextModule (node:internal/modules/esm/utils:337:16)
0.309     at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:163:18)
0.309     at callTranslator (node:internal/modules/esm/loader:430:14)
0.309     at ModuleLoader.moduleProvider (node:internal/modules/esm/loader:436:30)
0.309 
0.309 Node.js v22.2.0
------
failed to solve: process "/bin/sh -c node -e \"import('teraslice')\"" did not complete successfully: exit code: 1

Also to note when we run node -e "import('teraslice')" in node 18 and 20, we get the followingExperimentalWarning logs.

MacBook-Pro teraslice % node -e "import('teraslice')"
(node:96161) ExperimentalWarning: Import assertions are not a stable feature of the JavaScript language. Avoid relying on their current behavior and syntax as those might change in a future version of Node.js.
(Use `node --trace-warnings ...` to show where the warning was created)
(node:96161) ExperimentalWarning: Importing JSON modules is an experimental feature and might change at any time

Node 22.2.0 documentation on the topic can be read here. It states:

This feature was previously named "Import assertions", and using the assert keyword instead of with. Any uses in code of the prior assert keyword should be updated to use with instead.

https://nodejs.org/api/esm.html#import-attributes

The code in question is located here: https://github.com/terascope/teraslice/blob/aaeb0e9129d802f298bb04acc0f44b04617dafdf/packages/teraslice/src/lib/storage/backends/elasticsearch_store.ts#L13-L17

If I try to change the assert keyword to with as stated in the documentation, I get the error:

'with' statements are not allowed in strict mode.

Is there a better way we can import json files without this?

Also this is blocking issue #3605

sotojn commented 2 months ago

We may want to consider updating from typescript 5.2.2 to **5.3.***.

There is missing types that won't allow for the use of with until we move to at least 5.3 https://www.typescriptlang.org/docs/handbook/release-notes/typescript-5-3.html

godber commented 2 months ago

Here's the reasoning on why not to use with: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Strict_mode#simplifying_scope_management

I think we just need a new way to import json files. I think this is the "new" way after not being able to require them at some point.

sotojn commented 2 months ago

A possible solution would be to just get rid of json all together. As of right now those json files just hold schema objects for elasticsearch indicies. We could instead change the json files to ts files that export the schema, then just import them where needed. This should be cross compatible with nodes 18, 20, and 22.

jsnoble commented 2 months ago

we have no real reason to stick with json in this regards, I think what @sotojn suggested would be easiest, by just changing them into full .ts files that export an object. We should have less clashing with new/chaning features that way

godber commented 1 month ago

This is resolved in the PR above, now in release v1.8.0.