简体   繁体   English

如何在不使用“需要”或“导入”的情况下导入NPM模块?

[英]How do I import an NPM module without using 'require' or 'import'?

I know this is probably a very common and widely answered question. 我知道这可能是一个非常普遍且得到广泛回答的问题。 One of the reasons I know it, is because i've spent the last 3 hours figuring out how I shall be importing 3rd party libraries properly in my project which does not support the use of 'import' or 'require'. 我之所以知道其中一个原因,是因为我花了最后3个小时来弄清楚如何在我的项目中正确地导入第三方库,这不支持使用“导入”或“要求”。

It is most likely something very fundamental that i'm missing, but I have reached the point where i'm literally steaming with anger from not being able to find a solution. 很可能是我最想念的东西,但是我已经到了我无法找到解决方案的地步。

So my case ATM: 所以我的案例ATM:

I am currently working on a wordpress theme built on understrap. 我目前正在研究基于understrap的wordpress主题。 I have a dependency on https://www.npmjs.com/package/google-libphonenumber but I have no idea how to include it in my file, and I'm used to working in environments where i can just use import/require from node_modules. 我对https://www.npmjs.com/package/google-libphonenumber有依赖性,但是我不知道如何将其包含在文件中,而且我习惯于在只能使用import / require的环境中工作来自node_modules。

I've read that Browserify could be a solution and i've tried to make it work as a part of gulp, but I eventually just ended up with even more errors than before that were completely gibberish. 我已经读过Browserify可能是一个解决方案,并且我试图使它作为gulp的一部分工作,但最终我最终遇到的错误比以前更多的完全是乱码。

package.json 的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'));
}

Resulting in the following error: 导致以下错误:

Uncaught SyntaxError: Unexpected token {

and

require test 需要测试

var lib = require('libphonenumber-js');
$jq(function(){
  lib.isValidNumberForRegion('23123412', 'GB')
}

Resulting in the following error: 导致以下错误:

Uncaught ReferenceError: require is not defined

Load the script from unpkg 从unpkg加载脚本

<script src="https://unpkg.com/google-libphonenumber@latest"></script>

or from a local install 或从本地安装

<script src="node_modules/google-libphonenumber/dist/libphonenumber.js"></script>

From there you could see the libphonenumber as a global (in the window object) 从那里您可以看到libphonenumber是一个全局的(在window对象中)

Then follow the readme, but adapt it a bit: 然后遵循自述文件,但要对其稍作调整:

// Get an instance of `PhoneNumberUtil`.
//const phoneUtil = require('google-libphonenumber').PhoneNumberUtil.getInstance();
const phoneUtil = libphonenumber.PhoneNumberUtil.getInstance();

// Parse number with country code and keep raw input.
const number = phoneUtil.parseAndKeepRawInput('202-456-1414', 'US');

// Print the phone's country code.
console.log(number.getCountryCode());
// => 1

// Print the phone's national number.
console.log(number.getNationalNumber());
// => 2024561414

And see '2024561414' in your browser console 并在浏览器控制台中查看“ 2024561414”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM