Trying to require JavaScript module

I’ve this file in the root of my project:

x.js

alert('Hi');

When I import it via JavaScript, via require('./x') or require('x'), no alert appears.

JavaScript I need study this, my information is -2, but, I believe because the word (Hi) is in 2 accents ‘Hi’ (I don’t know how says this [ ’ ] ) and need this “Hi”.

Please, generally, if you do not know what you are saying, don’t answer. At best that’s just spammy for the OP’s notification, at worst it obscures right answers by drowning them among the misinformation, wastes the OP’s time on a lead that is wrong, can give the OP false misconceptions about GDevelop or the nature of their problem, and all in all make them fed up of GDevelop and quit it entirely.
I understand that you are trying to help and i respect that, but that kind of behavior can really have the opposite effect and be hurtful to a forum poster’s experience with GDevelop.


Now to answer the question: require is not part of JavaScript, but if NodeJS’s module API. It is therefore not really supported by GDevelop. The IDE and PC exports happen to run on electron which runs on NodeJS. That is why the require function appears to work, but it is not part of GDevelop. require Therefore won’t work. Even if you are only targeting electron builds, previews and builds are not containing all of your project files: It’s only the generated code files, the static code files, and the resources registered in your game json. Therefore even if you have a js file in your project folder, you cannot use require to import it, as it will not be present where the code will actually run.

Instead, if you want to use a library, you can paste its whole code into a javascript event.

If you want to use a JavaScript code file, that is not really possible with GDevelop. You could make a script to replace JS events content in the json with the contents of a JavaScript file I guess, but that’s not really worth the effort.

Note that JavaScript is meant as a quick and easy way to complement the events system in the few cases where it cannot do everything you need, for example, access a Web API that is not exposed to events. It is not meant as a primary scripting method for GDevelop.

1 Like

That makes sense. I was thinking require was supported because of this post.

Indeed, I was mistaken in that post.