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

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

问题描述

我是npm modulenode.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.

为此,在另一个问题中(

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);

问题是这是我第一次处理此问题.除了我是意大利人,英语说得不太好,而且在意大利语Google中,什么都没有:(

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 ?

推荐答案

是的,您可以直接在浏览器上使用npm模块.

Yeah, you can use npm modules directly on the browser.

浏览器是一个很好的选择.

直接从他们的页面上浏览:

Taken straight from their page:

浏览器没有定义require方法,但是Node.js有.和 Browserify可以使用以下方式编写使用require的代码: 您将在Node中使用它.

Browsers don't have the require method defined, but Node.js does. With Browserify you can write code that uses require in the same way that you would use it in Node.

现在您还有其他问题:

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

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

是的.但是,您只需要节点来安装browserify并将您的javascript捆绑到一个文件中,就可以直接将其包含在html中.因此,一旦有了捆绑的文件,就可以在没有节点的任何地方提供文件.

Yes. But you need node just to install browserify and to bundle your javascripts into a single file that you can include directly on the html. So, once you have the bundled file, you can serve it from anywhere without node.

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

Is it possible to use modules on local PC ?

是的!您可以在本地PC上执行几乎所有操作.例如,您可以将其用作开发用途的服务器,并在其中运行node.js服务器.

Yes! You can do pretty much anything on your local PC. You can use it as a server for development purposes and run a node.js server in it, for example.

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

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