DEV Community

Discussion on: Recap of Data Structures with Javascript Part 1

Collapse
 
bnorbertjs profile image
Norbert Braun • Edited

The easiest way to run the examples is parceljs.

Create an index.html in the same directory. Include your index.js in it like this.

<!DOCTYPE html>
<html>
<head>
</head>
<body>
    <script src="./index.js"></script>
</body>
</html>

Then install parcel with npm globally -> npm i -g parcel-bundler
After that, you can type "parcel index.html" in the console and it will be available on localhost:1234 and you can check the output in the console of the browser.

Parcel is a zero-configuration web application bundler by the way.

If you want to run it with node, you can do node ./index.js in the console, but it will fail because of the "import { blabla } from "something" syntax.

You can change the export statements to the following (example):

module.exports = {
 BinarySearchTree: BinarySearchTree,
 Node: Node
}

and then import it in your index.js
const { BinarySearchTree} = require("./BinarySearchTree")

If you don't want to change the code, you can setup babel with node as well.
(this is babel6, for babel7 please check the official documentation)

step1: npm -i babel-cli babel-preset-env
step2: create a .babelrc file that looks like this:

{
    "presets": ["env"]
}

step3: run your index.js with the following command:
./node_modules/.bin/babel-node index.js

Collapse
 
chenge profile image
chenge

Cool, I use parcel, thanks Braun really.