How to open popup menu on right click in Leaflet

your not opening the popup your just creating it, try

var popup = L.popup().setContent('<p>Hello world!<br />This is a nice popup.</p>'); 

this creates a popup then you need to make it open on a right click, the second argument to 'on' only accepts a function, that it will run with the first parameter being the event, in other words, the function can't end in parentheses,it's usually easier to just wrap what you want to do in an anonymous function as shown bellow.

map.on('contextmenu',function(){
    popup.openOn(map);
});

You could also do

map.openPopup(popup);

either is equivalent. make sure to avoid bindPopup because that assumes you want to use regular left clicks.

Edit: so to make it look more menu like you need to use a library to create a popup menu like one of these

map.on('contextmenu',function(e){
    plugin run!
});

Opens new popup on right click, don't forget to set the coordinate where the popup window should be placed by calling setLatLng function, otherwise you will get following error: leaflet.js:5 Uncaught TypeError: Cannot read property 'lat' of undefined at Object.project

map.on('contextmenu',(e) => {
  L.popup()
  .setLatLng(e.latlng)
  .setContent('<pre>Hello</pre>')
  .addTo(map)
  .openOn(map);
});

Tags:

Leaflet