jackocnr / intl-tel-input

A JavaScript plugin for entering and validating international telephone numbers
https://intl-tel-input.com
MIT License
7.38k stars 1.93k forks source link

Getting error while using utilsScript #1618

Closed udit-001 closed 2 months ago

udit-001 commented 2 months ago

Plugin version

v23.0.4

Expected behaviour

No errors should be there

Actual behaviour

Getting the following errors in browser console:

Brave Version 1.65.132 Chromium: 124.0.6367.202

Uncaught SyntaxError: Unexpected token 'export'

Firefox 125.0.3

Uncaught SyntaxError: export declarations may only appear at top level of a module

Initialisation options

I am not using webpack or any other bundler, I am using vanilla JavaScript and specifying the utilsScript as follows:

 utilsScript: "/static/js/utils.js",

Here's the script for reference:

var fieldDiv = document.querySelector("field-phone_number");
window.addEventListener('load', function (event) {
    var input = document.querySelector("#id_phone_number");
    var jsConfig = {
      initialCountry: "id", 
      separateDialCode: true,
      strictMode: true,
      onlyCountries: ["in", "id"]
    }
    const widgetConfig = {
        ...jsConfig,
        useFullscreenPopup: true,
        utilsScript: "/static/phones/js/utils.js",
        hiddenInput: function(telInputName) {
            return {
                phone: "phone_number_1"
            };
        }
    }
    window.intlTelInput(input, widgetConfig);
});

Comments

Should I include utils script in a different way in my project?

jackocnr commented 2 months ago

What version of the plugin are you using?

udit-001 commented 2 months ago

I'm using v23.0.4

jackocnr commented 2 months ago

Hmm that's strange. Do you see the same issue on the demo site?

udit-001 commented 2 months ago

No, I don't see that error on the demo site.

Can I use build/js/intlTelInputWithUtils.js instead of using a separate utils file?

jackocnr commented 2 months ago

Can I use build/js/intlTelInputWithUtils.js instead of using a separate utils file?

You can if you don't mind the filesize, as the utils script is aprx 260kb.

Or if you want to try and figure out this issue, it would help if you could find the minimal set of code that reproduces the problem and put that in a CodePen, so I can take a look. Here is an example pen with the plugin up and running - feel free to fork this if it helps.

udit-001 commented 2 months ago

Thank you for your help, the issue has been resolved.

It appears that the utils file was being included as a script tag as well. I was attempting to integrate this with Django, and there was a slight configuration issue, which caused the error to occur.