kestra-io / libs

3 stars 4 forks source link

Module error when using Javascript #9

Open FullStackIndie opened 1 week ago

FullStackIndie commented 1 week ago

I keep getting error when using the Javascript module and using

here is the error 2024-10-05 15:27:15.985Using task runner 'io.kestra.plugin.scripts.runner.docker.Docker' 2024-10-05 15:27:19.214Starting command with container id 3dbbecd088494911a2f47b8093bf5a3c45b8f0afc0e973790788d2e9d9c447f0 [/bin/sh -c set -e npm i @kestra-io/libs node /tmp/kestra-wd/tmp/1ZqEAZLo7y9Y3IJlth2u32/4712153262335601460.js] 2024-10-05 15:27:20.722 2024-10-05 15:27:20.722added 1 package in 1s 2024-10-05 15:27:20.724npm notice New minor version of npm available! 10.8.3 -> 10.9.0 2024-10-05 15:27:20.724npm notice 2024-10-05 15:27:20.724npm notice To update run: npm install -g npm@10.9.0 2024-10-05 15:27:20.724npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.9.0 2024-10-05 15:27:20.725npm notice 2024-10-05 15:27:20.762/tmp/kestra-wd/tmp/1ZqEAZLo7y9Y3IJlth2u32/4712153262335601460.js:315 2024-10-05 15:27:20.763at TracingChannel.traceSync (node:diagnostics_channel:315:14) 2024-10-05 15:27:20.763code: 'ERR_REQUIRE_ESM' 2024-10-05 15:27:20.763Instead either rename index.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 /tmp/kestra-wd/tmp/1ZqEAZLo7y9Y3IJlth2u32/node_modules/@kestra-io/libs/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead). 2024-10-05 15:27:20.763index.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. 2024-10-05 15:27:20.763at Object.<anonymous> (/tmp/kestra-wd/tmp/1ZqEAZLo7y9Y3IJlth2u32/4712153262335601460.js:1:16) { 2024-10-05 15:27:20.763} 2024-10-05 15:27:20.763undefined 2024-10-05 15:27:20.763

I tried using the require and import ES6 syntax and both did not work.

here is the error when using import

2024-10-05 15:29:19.621added 1 package in 846ms 2024-10-05 15:29:19.623npm notice 2024-10-05 15:29:19.623npm notice New minor version of npm available! 10.8.3 -> 10.9.0 2024-10-05 15:29:19.623npm notice Changelog: https://github.com/npm/cli/releases/tag/v10.9.0 2024-10-05 15:29:19.623npm notice To update run: npm install -g npm@10.9.0 2024-10-05 15:29:19.623npm notice 2024-10-05 15:29:19.674Reparsing as ES module because module syntax was detected. This incurs a performance overhead. 2024-10-05 15:29:19.674To eliminate this warning, add "type": "module" to /tmp/kestra-wd/tmp/1HNjMnlv4YbIleA7jXXEfV/package.json. 2024-10-05 15:29:19.674(Usenode --trace-warnings ...to show where the warning was created) 2024-10-05 15:29:19.674(node:18) [MODULE_TYPELESS_PACKAGE_JSON] Warning: Module type of file:///tmp/kestra-wd/tmp/1HNjMnlv4YbIleA7jXXEfV/2272223555352646281.js is not specified and it doesn't parse as CommonJS. 2024-10-05 15:29:19.679^^^^^^ 2024-10-05 15:29:19.679at async onImport.tracePromise.__proto__ (node:internal/modules/esm/loader:483:26) 2024-10-05 15:29:19.679SyntaxError: The requested module '@kestra-io/libs' does not provide an export named 'Kestra' 2024-10-05 15:29:19.679at async ModuleJob.run (node:internal/modules/esm/module_job:254:5) 2024-10-05 15:29:19.679file:///tmp/kestra-wd/tmp/1HNjMnlv4YbIleA7jXXEfV/2272223555352646281.js:1 2024-10-05 15:29:19.679import {Kestra} from '@kestra-io/libs'; 2024-10-05 15:29:19.679at ModuleJob._instantiate (node:internal/modules/esm/module_job:171:21) 2024-10-05 15:29:19.683Node.js v22.9.0

Since I was following Kestra documentation for JavaScript plugin npm will default to the latest package which is currently 0.20.0-SNAPSHOT

The temporary fix is to use the previous version in npm explicitly - "npm i @kestra-io/libs@0.12.0" To fix it, it looks like the code should be updated to ES6 syntax, remove type:"module" from the package.json, or rename index.js to index.cjs in order to continue using CommonJS with "type":"module" in package.json

smunteankestra commented 2 days ago

I am encountering an error when attempting to run the following Kestra flow, which is designed to execute a Node.js script using the @kestra-io/libs library within a Docker container.

id: myflow
namespace: company.team
tasks:
  - id: run_nodejs_script
    type: io.kestra.plugin.scripts.node.Script
    beforeCommands:
      - npm i @kestra-io/libs
    taskRunner:
      type: io.kestra.plugin.scripts.runner.docker.Docker
      image: node:slim
    warningOnStdErr: false
    logLevel: DEBUG
    script: |
      const Kestra = require('@kestra-io/libs');

error:

2024-10-14 10:00:57.059 npm notice New minor version of npm available! 10.8.3 -> 10.9.0 2024-10-14 10:00:57.094 /tmp/kestra-wd/tmp/2b3i1WNwnR38aPXbsHy4iy/12344889282504685718.js:315 2024-10-14 10:00:57.095 Error [ERR_REQUIRE_ESM]: require() of ES Module /tmp/kestra-wd/tmp/2b3i1WNwnR38aPXbsHy4iy/node_modules/@kestra-io/libs/src/index.js from /tmp/kestra-wd/tmp/2b3i1WNwnR38aPXbsHy4iy/12344889282504685718.js not supported. 2024-10-14 10:00:57.096 index.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. 2024-10-14 10:00:57.097 Instead, either rename index.js to end in .cjs, change the requiring code to use dynamic import(), or change "type": "module" to "type": "commonjs" in /tmp/kestra-wd/tmp/2b3i1WNwnR38aPXbsHy4iy/node_modules/@kestra-io/libs/package.json to treat all .js files as CommonJS. 2024-10-14 10:00:57.099 Node.js v22.9.0 2024-10-14 10:00:57.284 Command failed with exit code 1