{"record":{"id":"9046d4106526d922","repo":"nosir/cleave.js","slug":"cleave-js-please-include-phone-type-formatter-c","errorCode":null,"errorMessage":"[cleave.js] Please include phone-type-formatter.{country}.js lib","messagePattern":"\\[cleave\\.js\\] Please include phone-type-formatter\\.(.+?)\\.js lib","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/Cleave.js","lineNumber":165,"sourceCode":"        pps.maxLength = Cleave.Util.getMaxLength(pps.blocks);\n    },\n\n    initPhoneFormatter: function () {\n        var owner = this, pps = owner.properties;\n\n        if (!pps.phone) {\n            return;\n        }\n\n        // Cleave.AsYouTypeFormatter should be provided by\n        // external google closure lib\n        try {\n            pps.phoneFormatter = new Cleave.PhoneFormatter(\n                new pps.root.Cleave.AsYouTypeFormatter(pps.phoneRegionCode),\n                pps.delimiter\n            );\n        } catch (ex) {\n            throw new Error('[cleave.js] Please include phone-type-formatter.{country}.js lib');\n        }\n    },\n\n    onKeyDown: function (event) {\n        var owner = this,\n            charCode = event.which || event.keyCode;\n\n        owner.lastInputValue = owner.element.value;\n        owner.isBackward = charCode === 8;\n    },\n\n    onChange: function (event) {\n        var owner = this, pps = owner.properties,\n            Util = Cleave.Util;\n\n        owner.isBackward = owner.isBackward || event.inputType === 'deleteContentBackward';\n\n        var postDelimiter = Util.getPostDelimiter(owner.lastInputValue, pps.delimiter, pps.delimiters);","sourceCodeStart":147,"sourceCodeEnd":183,"githubUrl":"https://github.com/nosir/cleave.js/blob/a966c95cf5193f223485a10eec8405bb32b8f6dd/src/Cleave.js#L147-L183","documentation":"When `phone: true` is set, Cleave instantiates an AsYouTypeFormatter from the per-country libphonenumber addon script on `pps.root.Cleave`. If that addon file was not loaded, the constructor throws and Cleave replaces it with this clearer message at src/Cleave.js:165.","triggerScenarios":"new Cleave(el, { phone: true, phoneRegionCode: 'US' }) without first loading dist/addons/phone-type-formatter.us.js (or the equivalent script) into the same global root.","commonSituations":"Using the npm module without importing the addon file; bundler tree-shaking out the addon; loading the addon on a different global (window vs module scope); missing region-specific file for an uncommon country.","solutions":["Import the matching addon before constructing: require/import 'cleave.js/dist/addons/phone-type-formatter.{country}.js'.","Confirm the addon registers Cleave.AsYouTypeFormatter on the same global root Cleave uses (browser: window.Cleave).","If phone formatting is not needed, remove phone:true / phoneRegionCode from the options.","With a bundler, ensure the addon side-effect import is not tree-shaken."],"exampleFix":"// before\nconst Cleave = require('cleave.js');\nnew Cleave(el, { phone: true, phoneRegionCode: 'US' });\n// after\nconst Cleave = require('cleave.js');\nrequire('cleave.js/dist/addons/phone-type-formatter.us.js');\nnew Cleave(el, { phone: true, phoneRegionCode: 'US' });","handlingStrategy":"validation","validationCode":"if (typeof window.Cleave === 'undefined' || typeof window.Cleave.AsYouTypeFormatter !== 'function') {\n  throw new Error('Load phone-type-formatter.{country}.js before phone formatting');\n}","typeGuard":"function hasPhoneAddon(root) {\n  return !!(root && root.Cleave && typeof root.Cleave.AsYouTypeFormatter === 'function');\n}","tryCatchPattern":"try {\n  new Cleave(el, { phone: true, phoneRegionCode: 'US' });\n} catch (e) {\n  if (e.message.includes('phone-type-formatter')) {\n    console.warn('Phone addon missing; init without phone formatting');\n    new Cleave(el, {});\n  } else { throw e; }\n}","preventionTips":["Always import the region-matching addon alongside cleave.js when phone:true.","Side-effect imports (require/import of the addon) are not tree-shake-safe — mark them or keep them.","In tests/SSR, ensure the addon registers on the same global root the library reads.","Keep phoneRegionCode and the imported addon file country in sync."],"tags":["phone","missing-dependency","addon"],"backgroundTag":"missing-dependency","analyzedSha":"a966c95cf5193f223485a10eec8405bb32b8f6dd","analyzedAt":"2026-09-02T23:44:35.488Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-10T02:17:09.455Z"}