Trying to use ES Modules with Alloy

I created a new Alloy project and followed the example from Getting Started with Alloy // Pebble Developers
i.e. I added a new file math.js in the same folder as main.js with the content

export function add(a, b) {
    return a + b;
}

export function multiply(a, b) {
    return a * b;
}

and replaced the code in main.js with

import { add, multiply } from "./math";

console.log("Sum: " + add(2, 3));       // 5
console.log("Product: " + multiply(4, 5));  // 20

It compiled and installed successfully, but the log shows the following problem:

$ pebble logs --emulator emery
[19:31:24] xsHost.c:155> Found mod "pebble.moddable.tech"
[19:31:24] xsRun.c:408> Exception SyntaxError: import add not found

How can I make it work?

1 Like

I’m a bit lazy to check that it actually works, so I might be wrong.

But have you seen this example which imports from another file?

3 Likes

Thanks for the hint! I had seen this example before, but after looking at it again I finally found what was missing:

After modifying src/embeddedjs/manifest.json and replacing

	"modules": {
		"*": "./main.js"
	}

with

	"modules": {
		"*": [
			"./main",
			"./math.js"
		]
	}

it finally worked :slight_smile:

Maybe this should be added to the documentation?

1 Like