Server javascript code

Recommended Posts


I was looking for some javascript pathfinding libraries and wanted to use such for my game but I realize they use the require function and what I gathered is that it means the script can only be used in server (NodeJS).

So, what are my best options if I want to use such libarary?

I'm really a beginner to server side javascript and don't know what it takes to get that running


Share this post

Link to post
Share on other sites

Rightly or wrongly, most JS applications now use a build system of some sort, which means they have access to a module system, which means that they can understand `require` (which is CommonJS format, as used by Node, to complicate things, there is now ES6 module spec as well, using `import` and `export` syntax, mostly, build systems will understand both, mostly).

As most JS apps use a build system this means that most JS libraries (such as found on npm, which is *not* just for node, its for JS, which includes node and client and IOT and and and etc etc etc) now _expect_ that build system and the module system that it allows. However, many many JS libraries also expose a client-only version of their library for those who do not want to use a build system.

Even if you are building an entirely client-side application I would still advise setting up a build tool chain.

Some disadvantages of a build chain:

* Can be tricky to set up

* Still not quite as simple as just running code in the browser

Some advantages:

* Many cross-browser issues disappear due to transpilation

* You get access to a module system, helping you to organise your code base

* Due to module system you get easier access to external libraries by using a service such as npm to access that code (i.e. you no longer have to reinvent the wheel and it is considerably safer than other methods of accessing third-party code)

If you did want to go down the route of using a build system I think the following steps would get you there, usually without too much trouble, but, it might involve quite a lot of learning depending on how experienced you are:

* Install node on your local machine, this side-installs npm, the node package manager.

* Install a command line on your machine (if it does not already have it) and spend a few hours understanding how to use it.

* Create a new folder for your new project.

* From the command line run `npm init`, this sets up a new project.

* From the command line run `npm install -g parcel-bundler`

* Create an `index.html` file and put whatever you like in there

* From the command line run `parcel index.html`

At this point 1 of 2 things will happen:

1) You will have port access issues on _some_ systems. Resolution to this depends on your machine, google the solution, there will be lots of solutions.

2) You local server will fire up and will serve your index.html.

If you run in to point 1, fix it, and you will be at point 2. This is good.

Now cancel that running script (usually ctrl-C/cmd-C from the command line).

From the command line run `parcel watch index.html`.

Now you have a development build running.

Change the code within index.html, switch back to your browser tab, see those changes instantly propagated in the browser _without_ a page refresh. It is magic.

Beyond installing node (and npm), these steps are outlined in more detail at https://parceljs.org/getting_started.html.

Parcel is one of many bundlers you may use, but it is the easiest to get going with (and very good I might add, but you have choices if you don't like it).

Note that none of this is non-trivial, and its up to you if you think the advantages (which I have barely touched on) outweigh the initial cost of setup.

Also note that this isn't necessarily the 'best' way to get started, I'd advise some changes, but, you can do those later. This is likely the easiest way to get going with build tooling for JS.

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.