-1

This is my implementation of a SinglyLinkedList,

export default class SinglyLinkedList{

    constructor(){
        this.head = null;
        this.size = 0;
    }

    insert(item){
        this.size ++;
        let p = new Node(item);
        if(this.head === null){
            this.head = p;
        }
        else{
           p.next = this.head;
           this.head = p;
        }
    }

    remove(item){
        let curr, prev;
        prev = curr = this.head;
        if(item === curr.val){
            this.head = this.head.next;
            this.size --;
            return;
        }
        while(curr !== null){
            if (curr.val === item){
                prev.next = curr.next;
                this.size --;
                return;
            }
            prev = curr;
            curr = curr.next;

        }
        throw "Item doesn't exist in list."
    }
    length(){
        return this.size;
    }

    printList(){
        let curr = this.head;
        let out = [];
        while(curr !== null){
            if(curr.next === null){
                out.push(curr.val);
            }else{
                out.push(curr.val);
                out.push("->")
            }
            curr = curr.next;
        }
        return out.join("");
    }
}

class Node{
    constructor(val){
        this.val = val;
        this.next = null;
    }
}

This code works if I put the calling code in the same file as the implementation of the data structure, however I want to separate the calling code into a different file.

I'm calling this in main.js in the following manner,

import SinglyLinkedList from './singlyLinkedList';


const list = new SinglyLinkedList();
list.insert(12);
list.insert(9);
list.insert(13);
list.insert(17);
console.log(list.printList());
list.remove(12);
console.log(list.printList());
console.log(list.length());
console.log(list.is_empty());
list.insertAt(21, 2);
console.log(list.printList());

When I run the file using node, it fails gloriously with the following error,

$|~/Dropbox/js/data-structures-js |=>node main.js 
/Users/melissa/Dropbox/js/data-structures-js/main.js:1
(function (exports, require, module, __filename, __dirname) { import SinglyLinkedList from './singlyLinkedList';
                                                                     ^^^^^^^^^^^^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:83:7)
    at createScript (vm.js:267:10)
    at Object.runInThisContext (vm.js:319:10)
    at Module._compile (internal/modules/cjs/loader.js:684:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:774:12)
    at executeUserCode (internal/bootstrap/node.js:342:17)

What am I doing wrong here and why can't javascript be a little human friendly?

Melissa Stewart
  • 3,483
  • 11
  • 49
  • 88
  • 3
    *"...and why can't javascript be a little human friendly?"* Node.js != JavaScript. This has nothing to do with JavaScript being friendly. It has to do with how Node.js implements ESM. Now, it would be nice if **Node.js** provided a friendlier error message, but that's true for a lot of Node.js. – T.J. Crowder Jan 12 '19 at 17:49
  • Support for ES modules is gated by an `--experimental-modules` flag. Please see: https://nodejs.org/api/esm.html for how to enable it. Also note you will have to the MJS extensions. – Tomáš Hübelbauer Jan 12 '19 at 19:05
  • Basically its not working with import\export like this. you can use require or use babel to compile it to es5 – Talg123 Jan 12 '19 at 19:16
  • instead of enabling ESM with a flag you can just rewrite your code to match older [`require()/exports` approach](https://nodejs.org/api/modules.html) – skyboyer Jan 12 '19 at 19:25

1 Answers1

1

Node.js's current (experimental) support for ESM¹ syntax requires that your modules (both the importing and exporting ones) use the extension mjs, to distinguish them from Node's traditional CommonJS-like modules. So main.js => main.mjs, SinglyLinkedList needs to be in singlyLinkedList.mjs, etc. You also need to use the --experimental-modules flag (and, obviously, a very recent version of Node.js).

More in the documentation.


¹ = "ESM" = "ECMAScript Module", to distinguish it from other module styles, such as CommonJS, SystemJS, AMD, ...

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875