ES6进口惨败

时间:2019-01-12 17:45:58

标签: javascript ecmascript-6

这是我对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;
    }
}

如果将调用代码与实现数据结构的文件放在同一文件中,则此代码有效,但是我想将调用代码分离到另一个文件中。

我以以下方式在main.js中称呼它,

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

当我使用节点运行文件时,它因以下错误而严重失败

$|~/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)

我在这里做错什么了,为什么JavaScript不能对人类友好一点?

1 个答案:

答案 0 :(得分:1)

Node.js当前对ESM¹语法的(实验性)支持要求您的模块(导入和导出模块)都使用扩展名mjs,以使其与Node的传统类CommonJS模块区分开。因此main.js => main.mjsSinglyLinkedList必须在singlyLinkedList.mjs中,依此类推。您还需要使用--experimental-modules标志(显然,最新版本的Node.js)。

the documentation中的更多内容。


¹=“ ESM” =“ ECMAScript模块”,以区别于其他模块样式,例如CommonJS,SystemJS,AMD,...