Skip to content

Javascript / Module support #184

Open
@aheissenberger

Description

@aheissenberger

This works with bash helper (code at end of this note):

import {readFile} from "node:fs/promises"
const content = await readFile('test.txt',{encoding:'utf8'})

but when I add the functions to import/export the Data it fails:

import {readFile} from "node:fs/promises"
let transform = DM_getPanel(0);
const content = await readFile('test.txt',{encoding:'utf8'})

it fails with require is not defined in ES module scope - I think you use require in your wrapper or macro:

Error: [INFO] 2022-02-24T09:57:49 DataStation Runner (Go) development
[INFO] 2022-02-24T09:57:49 Evaling program panel
(node:4156) ExperimentalWarning: Fetch is an experimental feature. This feature could change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
(node:4156) ExperimentalWarning: --experimental-loader is an experimental feature. This feature could change at any time
file:///private/var/folders/nq/kqg00wpj0yqcdzpzgts7ww440000gn/T/program-panel-792598270:7
  const fs = require('fs');
             ^

ReferenceError: require is not defined in ES module scope, you can use import instead
    at DM_getPanel (file:///private/var/folders/nq/kqg00wpj0yqcdzpzgts7ww440000gn/T/program-panel-792598270:7:14)
    at file:///private/var/folders/nq/kqg00wpj0yqcdzpzgts7ww440000gn/T/program-panel-792598270:30:17
    at ModuleJob.run (node:internal/modules/esm/module_job:197:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:341:24)
    at async loadESM (node:internal/process/esm_loader:88:5)
    at async handleMainPromise (node:internal/modules/run_main:61:12)

Node.js v17.6.0
[INFO] 2022-02-24T09:57:49 Failed to eval: exit status 1

    at evalInSubprocess (/Applications/DataStation Community Edition.app/Contents/Resources/app.asar/desktop/panel/eval.ts:190:27)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at IpcMainImpl.<anonymous> (/Applications/DataStation Community Edition.app/Contents/Resources/app.asar/desktop/rpc.ts:83:9)

1.) I suggest to implement it like an extra language or if possible as an extra option in the settings file (type: module) as it will require a new wrapper.
2.) wouldn't it be better to provide the functions (e.g. DM_getPanel) in a module which needs to be imported e.g. import {DM_getPanel} from 'datastation'

I used this bash script with experimental-loader to forces node to load file as module:

#!/usr/bin/env bash
set -e
exec /opt/homebrew/bin/node --experimental-loader 'data:text/javascript,let%20e=true;export%20function%20load(t,o,r)%7Bif(e)%7Bo.format=%22module%22;e=false%7Dreturn%20r(t,o,r)%7D' --experimental-fetch "$@"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions