Skip to content

ESM issues

fast-alfred by default bundles your scripts in the cjs format.
In case your script uses ESM syntax, you might encounter the following error:

log
SyntaxError: Cannot use import statement outside a module

Solution

Make sure no ESM syntax is used in your build

fast-alfred provides nice helpers to EMS syntax, in order to have compatibility with CommonJS packages.
Make sure this option disabled in case you're using the cjs format.
Also, verify that the outputFormat is set to cjs.

javascript
/**
 * @type {import('fast-alfred').FastAlfredConfig}
 */
module.exports = {
    bundlerOptions: {
        // Both should be the default
        esmHelpers: false, // Disable compatibility for CommonJS packages
        outputFormat: 'cjs', // Output files in the CommonJS format
    },
}

Migrate to ESM

You can change the output format to esm by adding the following configuration to your .fast-alfred.config.cjs file.

NOTE ⚠️

Your package.json should have "type": "module" in order to use the ESM format.

javascript
/**
 * @type {import('fast-alfred').FastAlfredConfig}
 */
module.exports = {
    bundlerOptions: {
        outputFormat: 'esm', // Change the output format to ESM
        esmHelpers: true, // Enable compatibility for CommonJS packages - not mandatory, but recommended
    },
}

Released under the MIT License