如何在浏览器中使用 npm 模块?即使在本地(PC)中也可以使用它们?- javascript [英] How to use npm modules in browser? is possible to use them even in local (PC) ? - javascript

查看:53
本文介绍了如何在浏览器中使用 npm 模块?即使在本地(PC)中也可以使用它们?- javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 npm 模块node.js 的新手,所以对我来说真的很难.

I'm new to npm module and node.js so it is really difficult to me.

我有一个带有很多点的 js 代码,对于每个点,我都想获得最近的城市.

I have a js code whit many points and for each one of them I want to get the nearest city.

为此,在其他问题中(使用大数组进行反向地理编码是最快的方法? - javascript 和性能),一位用户建议我使用两个 npm 模块

To do this, in other question (Reverse geocoding with big array is fastest way? - javascript and performance), a user suggested me to use two npm modules,

const kdbush = require('kdbush');
const geokdbush = require('geokdbush');

// I've stored the data points as objects to make the values unambiguous
const cities = [
  { name: "Abano Terme (PD)", latitude: 45.3594, longitude: 11.7894 },
  { name: "Abbadia Cerreto (LO)", latitude: 45.3122, longitude: 9.5928 },
  { name: "Abbadia Lariana (LC)", latitude: 45.8992, longitude: 9.3336 },
  { name: "Abbadia San Salvatore (SI)", latitude: 42.8800, longitude: 11.6775 },
  { name: "Abbasanta (OR)", latitude: 40.1250, longitude: 8.8200 }
];

// Create the index over city data ONCE
const index = kdbush(cities, ({ longitude }) => longitude, ({ latitude }) => latitude);

// Get the nearest neighbour in a radius of 50km for a point with latitude 43.7051 and longitude 11.4363
const nearest = geokdbush.around(index, 11.4363, 43.7051, 1, 50);

问题是这是我第一次接触这个.此外,我是意大利人,英语不太好,在意大利语谷歌中什么都没有:(

The problem is this is the first time that I approach at this. Besides I'm Italian and don't speak English very well, and in Italian Google there's nothing :(

你能告诉我如何使用这些模块吗?

Can you tell me how could I use these modules?

我必须在我的服务器上安装 Node.js 吗?

Do I have to install Node.js on my server ?

是否可以在本地 PC 上使用模块?

Is it possible to use modules on local PC ?

推荐答案

browserify 是正确的方向,但我花了相当多的努力才找到实际的解决方案.我为此总结了一个短博客,这里有一些快速回顾:

browserify is the correct direction, but it took me quite some effort to work out the actual solution. I have summarized a short blog for this, and here are some quick recap:

假设您想在 HTML 中使用 emailjs-mime-parserbuffer npm 库.

Say, you want to use emailjs-mime-parser and buffer npm libraries in your HTML.

  1. 安装所需的一切

npm install -g browserify
npm install emailjs-mime-parser
npm install buffer

  1. 编写一个简单的 main.js 作为包装器:
  1. write a simple main.js as a wrapper:

var parse = require('emailjs-mime-parser').default
var Buffer = require('buffer').Buffer
global.window.parseEmail = parse
global.window.Buffer = Buffer

  1. 使用browserify
  2. 编译一切

browserify main.js -o bundle.js

  1. 现在,您可以在 HTML 文件中使用 bundle.js.

<html>
<head>
<script src='bundle.js'></script>
<script>
console.log(window.parseEmail);
console.log(window.Buffer);
</script>
<body>
</body>
</html>

这篇关于如何在浏览器中使用 npm 模块?即使在本地(PC)中也可以使用它们?- javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆