As Douglas says, you need to change imports.searchPath
to include the location of your library. Use .
simple, but depends on files that always run from the same directory. Unfortunately, finding the directory of the current executable script is a huge hack. Here Gnome Shell does this for extension APIs
I applied this in the following function for general use:
const Gio = imports.gi.Gio; function getCurrentFile() { let stack = (new Error()).stack; // Assuming we're importing this directly from an extension (and we shouldn't // ever not be), its UUID should be directly in the path here. let stackLine = stack.split('\n')[1]; if (!stackLine) throw new Error('Could not find current file'); // The stack line is like: // init([object Object])@/home/user/data/gnome-shell/extensions/ u@u.id /prefs.js:8 // // In the case that we're importing from // module scope, the first field is blank: // @/home/user/data/gnome-shell/extensions/ u@u.id /prefs.js:8 let match = new RegExp('@(.+):\\d+').exec(stackLine); if (!match) throw new Error('Could not find current file'); let path = match[1]; let file = Gio.File.new_for_path(path); return [file.get_path(), file.get_parent().get_path(), file.get_basename()]; }
Here you can use it from your app.js
entry point file after defining the getCurrentFile
function:
let file_info = getCurrentFile(); // define library location relative to entry point file const LIB_PATH = file_info[1] + '/lib'; // then add it to the imports search path imports.searchPath.unshift(LIB_PATH);
In and! Now importing our libraries is very simple:
// import your app libraries (if they were in lib/app_name) const Core = imports.app_name.core;
source share