Import error with unexpected token
Hello,
In my current plugin layout, designed to be modular, the main file simplicior.qml
lives in a folder simplicior
on the plugin search path. At line 27, it pulls in a JavaScript file as:
import "./src/key.js" as Key
and the key.js
itself pulls in a symbol from a helper:
import { log } from "./src/helpers.js";
The symbol log
is exported from the helpers.js
file. Unfortunately, I get the following error:
09:19:28.857 | ERROR | main_thread | ExtensionBuilder::load | "file:///home/peter/Documents/MuseScore4/Plugins/simplicior/simplicior.qml:27 Script file:///home/peter/Documents/MuseScore4/Plugins/simplicior/src/key.js unavailable\nfile:///home/peter/Documents/MuseScore4/Plugins/simplicior/src/key.js:1 Unexpected token `import'\nfile:///home/peter/Documents/MuseScore4/Plugins/simplicior/src/key.js:1 Unexpected token `string literal'\n"
For some reason a \n
made it into the URI with a file
scheme. I suspect that the expression should have read:
import file:///home/peter/Documents/MuseScore4/Plugins/simplicior/src/key.js
Note, with a space instead of a \n
. Is that a bug or a feature, or are my assumptions on imports wrong?
Kind regards,
peter
Comments
Just forgot to mention, problem was observed in MuseScore-Studio-4.5.1.250800846-x86_64.AppImage on Ubuntu 22.04.2 LTS.
To reproduce, on a command shell:
Enable plugin, load a score and run the plugin notation/Simplicior Notation
For the records. Through trial and error I think it has become clear that an imported javascript file cannot in turn import another file. I wonder if this limitation to modular software design is intended, comes with the
QmlEngine
and could be overcome somehow.https://doc.qt.io/archives/qt-5.9/qtqml-javascript-resources.html
https://doc.qt.io/archives/qt-5.9/qtqml-javascript-imports.html
In reply to https://doc.qt.io/archives… by msfp
@msfp, many, many thanks; this made a real difference, just one character, a
.
in front of theimport
.Response:
The error suggests that the JavaScript module import syntax (import { log } from "./helpers.js") is not being interpreted correctly in your QML/JS environment. QML's JavaScript engine has limited support for ES6-style module imports—it typically expects traditional Qt.include() or relative path imports without import statements.
Instead of using ES6 imports in key.js, try:
For helper functions: Use Qt.include("helpers.js") and access log as a global symbol.
For modularity: Move shared code to a .pragma library JS file or restructure imports in QML (e.g., import "src" as Helper).
The \n in the error is likely a red herring; the core issue is unsupported import syntax. Check Qt/QML’s documentation for supported JS features in your version.
Let me know if you need further clarification!
https://bazi-lab.com/
https://yt-transcript.com
In reply to Response: The error suggests… by zsmalcor
Thanks for the comprehensive response.
For now I am importing the javascript using a
.import
and that seems to work.Kind regards, peter