After spending hours searching for a solution, I am still struggling with importing third-party libraries into my project that does not support 'import' or 'require'. It's frustrating to hit a roadblock like this, especially when working on a WordPress theme understrap.
Specifically, I need to include google-libphonenumber in my project, but I'm unsure how to do it without the familiar 'import' or 'require' commands I'm used to.
While some sources suggest using Browserify, my attempts to integrate it into my gulp workflow only resulted in more confusing errors.
package.json
"dependencies": {
"@babel/preset-env": "^7.4.5",
"bootstrap": "^4.3.1",
"browser-sync": "^2.26.7",
"css-element-queries": "^1.2.0",
"del": "^4.1.0",
"font-awesome": "^4.7.0",
"gulp": "^3.0.0",
"gulp-autoprefixer": "^6.0.0",
"gulp-clean-css": "^4.0.0",
"gulp-concat": "^2.6.1",
"gulp-ignore": "^2.0.2",
"gulp-imagemin": "^5.0.3",
"gulp-minify": "^3.1.0",
"gulp-plumber": "^1.2.1",
"gulp-rename": "^1.4.0",
"gulp-replace": "^1.0.0",
"gulp-rimraf": "^0.2.2",
"gulp-sass": "^3.0.2",
"gulp-sequence": "^1.0.0",
"gulp-sourcemaps": "^2.6.5",
"gulp-uglify": "^3.0.2",
"gulp-watch": "^5.0.1",
"javascript-detect-element-resize": "^0.5.3",
"jquery": "^3.4.1",
"libphonenumber-js": "^1.7.21",
"run-sequence": "^2.2.1",
"undescores-for-npm": "^1.0.0"
}
import test
import { getPhoneCode } from 'libphonenumber-js';
$jq(function(){
console.log(getPhoneCode('GB'));
}
This resulted in an error:
Uncaught SyntaxError: Unexpected token {
require test
var lib = require('libphonenumber-js');
$jq(function(){
lib.isValidNumberForRegion('23123412', 'GB')
}
Which led to the following error:
Uncaught ReferenceError: require is not defined