How to retrieve objects from .mjs files

Hi there and thx for reading and answering this post.

I just declare a basic variable like

var x = 2

in a .mjs file. ( Javascript module file)

then I import the .mjs file in my C++ application with

importModule("mylib.mjs");

So far so good, but before qt 4.14, I was able to retrieve x with e.g.

var y = module.x

But now, I can’t get x anymore.

Have an idea ??

Thx for answering

I add that

var y = mylib.x

doesn’t work either …:frowning:

I don’t work with Qt and I find odd that a simple var declaration can be accessed from the outside. Anyways, I looked up and found this syntax:

import * as mylib from "mylib.mjs";

mylib.x

No, I won’t directly import from JS script, but from my C++ app with importModule()

The syntax above is for QML: https://doc-snapshots.qt.io/qt5-dev/qtqml-javascript-imports.html

If you’re not using QML, try using the export keyword before the var declaration, which is the proper usage anyways. See: https://doc-snapshots.qt.io/qt5-5.12/qjsengine.html

Added export to my var declaration but it doesn’t change anything, sorry…

I also upgraded to Qt 14.1, but no way with or without the export keyword

Maybe this can help: https://forum.qt.io/topic/107606/qjsengine-cannot-use-import-syntax/3

YES, it helps…:slight_smile:

OK I got it. It works

Thx again for your help