1

When using a Leaflet plugin in Angular 18, everything works fine during development (serve time). However, after building the project, the JS file doesn't load, causing an error.

For example, with Leaflet.EasyButton this error happened:

C.easyButton is not a function

I tried adding the JS file to angular.json, including it in allowedCommonJsDependencies, and disabling optimization, but none of these solutions worked.

import JS file:

import 'public/js/easy-button.js';

It seems the JS file is not loaded in the correct order in the built files.

1
  • you say "I tried adding the JS file to angular.json, including it in allowedCommonJsDependencies". NOT, it's in architect-->build-->options-->scripts or import * as Easy from 'public/js/easy-button.js'; Commented Dec 5 at 7:14

1 Answer 1

0

I had this, too. In fact, I first found this page trying to find a solution for an h.easyButton is not a function error. What worked for me in the end was just calling it via window.L.easyButton instead of L.easyButton.

Gemini explained is thus:

  • Your Module code sets window.L = L.
  • The plugin loads, sees window.L, and attaches .easyButton to it.
  • Crucially: Because your local L (aka h) is a frozen Namespace Object, it does not receive that new property. It remains a pure view of the leaflet exports.
  • So, window.L.easyButton exists. L.easyButton (or h.easyButton) does not.

This is a specific behavior of ESBuild (the new Angular build system) combined with ES Modules.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.