Javascript.NodeJS icon indicating copy to clipboard operation
Javascript.NodeJS copied to clipboard

ES modules InvokeFromStringAsync

Open stephenlautier opened this issue 1 year ago • 2 comments

Hi,

I'm trying to invoke an .mjs or ES module from a string is this somehow possible?

I've seen this issue and from file should be supported https://github.com/JeringTech/Javascript.NodeJS/issues/151

However in this case, I'm using a class library and the file is an Embedded Resource, so then its invoked with InvokeFromStringAsync

[10:54:39 WRN] [Jering.Javascript.NodeJS.HttpNodeJSService] An invocation attempt failed. Retries using existing process remaining: 0.
Exception:
  Jering.Javascript.NodeJS.InvocationException: Unexpected token 'export'
anonymous:2
export default async (
^^^^^^

SyntaxError: Unexpected token 'export'
    at internalCompileFunction (node:internal/vm:73:18)
    at wrapSafe (node:internal/modules/cjs/loader:1274:20)
    at Module._compile (node:internal/modules/cjs/loader:1320:27)
    at IncomingMessage.<anonymous> (file:///S:/git/rig/midgard.pwa/[eval1]:170:21)
    at IncomingMessage.emit (node:events:517:28)
    at endReadableNT (node:internal/streams/readable:1400:12)
    at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
   at Jering.Javascript.NodeJS.HttpNodeJSService.TryInvokeAsync[T](InvocationRequest invocationRequest, CancellationToken cancellationToken)
   at Jering.Javascript.NodeJS.OutOfProcessNodeJSService.TryInvokeCoreAsync[T](InvocationRequest invocationRequest, CancellationToken cancellationToken)

Any help would be appreciated thank you 🙇‍♂️

stephenlautier avatar Sep 10 '24 09:09 stephenlautier

From the error it seems you may have used module.export instead of module.exports? <-- Note the "s" at the end of exports

Curlack avatar Oct 24 '24 19:10 Curlack

No its an esm not cjs (so no exports)

the export is export default async (

I found a solution to this and might be better actually in this case

Basically having the mjs file as EmbededResource -> convert to string -> write file (in host) and invoke it as a file - ESM is supported that way and it avoids the transfer of the script as string + compile which is done in this library (so I believe its more efficient)

stephenlautier avatar Oct 25 '24 13:10 stephenlautier