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

Is there a option to use import/export syntax? #147

Closed
entropin opened this issue Sep 6, 2022 · 3 comments · Fixed by #154
Closed

Is there a option to use import/export syntax? #147

entropin opened this issue Sep 6, 2022 · 3 comments · Fixed by #154

Comments

@entropin
Copy link

entropin commented Sep 6, 2022

Hi I have built some javascript using vue and Vite (ES modules), I'm trying to use Javascript.NodeJs to run the server-side rendering part of the javascript on our IIS Server.

cmd Node handles the file just fine, also using c# and Process Start() and pointing Node to the file path works. I just need to add a package.json and specifically enable modules. But I can't find a way to run the script using this lib. Is there a way?

Am about to give up, but found this comment:

"ES6 modules (import/export syntax) are experimental in the latest Node.js version (v14.4.0): nodejs.org/api/esm.html. To enable it, you can pass special arguments to Node.js or add an element to your package.json - refer to the Node.js docs enabling section for details. Documentation on passing arguments to Node.js through Jering.Javascript.Nodejs: github.com/JeringTech/Javascript.NodeJS#nodejsprocessoptions."

But I can't see a way to run it, we are using Node 18, can this script be run without having to use Bable to transpile? Any pointers?

@ianllewellyn
Copy link

@entropin FYI if you haven't found it already you can set Vite 3 to use common js for ssr with the ssr.format setting:
https://vitejs.dev/config/ssr-options.html#ssr-format

I had the same issue myself upgrading from Vite 2 and settled on this for now.

You could also set rollup options format to use commonjs (build.rollupOptions.output.format) but that will affect your client build as well, which probably isn't what you want. If you set the ssr.format it will leave your client build alone.

@JeremyTCD JeremyTCD linked a pull request Jan 18, 2023 that will close this issue
@JeremyTCD
Copy link
Member

JeremyTCD commented Jan 18, 2023

Apologies for the slow response. I've added support for .mjs files when using InvokeFromFileAsync/InvokeFromFileAsync<T>.

Word of caution - use of .mjs files is flaky with older versions of Node.js (tried with 18.12.1 and 18.13.0). These Node.js versions randomly crash when the dynamic import is attempted (import(<mjs path>)), without throwing any errors. No issues with 19.4.0 though.

I will be releasing a version of the library with the addition some time this week.

Edit: crashing is not an issue if you are consuming this library as a Nuget package. See #151 (comment).

@lukedukeus
Copy link

@JeremyTCD Can you post an example consuming an npm package inside .mjs file? I am trying to get it working and am seeing the following error:

Jering.Javascript.NodeJS.InvocationException: 'require() of ES Module \bin\Debug\net7.0\implementation\interop.mjs not supported.
Instead change the require of \bin\Debug\net7.0\implementation\interop.mjs to a dynamic import() which is available in all CommonJS modules.
Error [ERR_REQUIRE_ESM]: require() of ES Module \bin\Debug\net7.0\implementation\interop.mjs not supported.
Instead change the require of \bin\Debug\net7.0\implementation\interop.mjs to a dynamic import() which is available in all CommonJS modules.
    at Server.<anonymous> ([eval]:1:1374)
    at Generator.next (<anonymous>)
    at [eval]:1:355
    at new Promise (<anonymous>)
    at o ([eval]:1:100)
    at IncomingMessage.<anonymous> ([eval]:1:557)
    at IncomingMessage.emit (node:events:539:35)'

package.json:

{
  "name": "implementation",
  "version": "1.0.0",
  "main": "index.js",
  "type": "module",
  "dependencies": {
    "@cortex-js/compute-engine": "^0.12.2"
  }
}

interop.js:

import { parse } from "@cortex-js/compute-engine";

const evaluate = (callback, latex1, latex2) => {
  const parsed1 = parse(latex1);
  const parsed2 = parse(latex2);

  const result = true;
  callback(null, result);
};

module.exports = { evaluate };

driver.cs:

bool result = await _node.InvokeFromFileAsync<bool>("implementation/interop.mjs", "evaluate", args: new object[] { latex1, latex2 });

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 a pull request may close this issue.

4 participants