This part of your code is all mixed up as you should only have the one onReady page function and the imports should all be at the top of your page.
import wixLocation from 'wix-location';
// ...
wixLocation.onChange( (location) => {
let newPath = location.path;
} );
$w.onReady(function () {
$w("#dataset2").onBeforeSave(function() {
$w("#dataset2").setFieldValue('pageUrl', wixLocation.url);
});
});
import {sendEmail} from 'backend/email';
$w.onReady(function () {
$w("#dataset2").onAfterSave(sendFormData);
});