How to use npm modules in browser? is possible to use them even in local (PC) ? - javascript

断了今生、忘了曾经 提交于 2019-12-06 01:33:59

问题


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

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

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?

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

Is it possible to use modules on local PC ?


回答1:


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

Browserify is an excelent option for that.

Taken straight from their page:

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.

Now your other questions:

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

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.

Is it possible to use modules on local PC ?

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.




回答2:


Yes, you must download node.js. follow these directions: https://www.npmjs.com/get-npm "npm is distributed with Node.js- which means that when you download Node.js, you automatically get npm installed on your computer."

So download Node.js (follow the steps at the above link to see if you have it already). then use the CLI (Command Line Interface) to find your source component directory. Once you are within that directory use npm commands to install the package (which has the modules you need). Depending on what library or framework you are using will dictate how you use these modules. If you save them locally, as I'm describing, then base on your directory path you can access the modules: require('.path/to/your/npm_modules/example.js');

I hope this helps. Non ho tanto tempo allora non posso scrivere di piu'. Io parlo Italiano, ma non ho parlato di computer spesso. Buona fortuna.



来源:https://stackoverflow.com/questions/49562978/how-to-use-npm-modules-in-browser-is-possible-to-use-them-even-in-local-pc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!