0

I'm trying to use the ES6 import syntax and keep running into errors. This is my data type,

class UnionFind{
    constructor(n){
        this.items = n;
    }

    union(p, q){

    }

    connected(p, q){

    }

    find(p){

    }

    count(){

    }

}
export default UnionFind

Saved in a file UnionFind.js

This the calling client,

import { UnionFind } from './unionFind';

const readline = require('readline');

const rl = readline.createInterface({
    input:process.stdin,
    output:process.stdout,
    terminal:false

});

uf = new UnionFind(10)

rl.on('numbers', function (line) {
    arr = number.split(' ')

    console.log(arr);
});

This is saved in a file client.mjs

This is how I'm running it,

node --experimental-modules union-find/client.mjs

I get the following error,

(node:13465) ExperimentalWarning: The ESM module loader is experimental.
file:///Users/mstewart/Dropbox/data-structures-algorithms-princeton/union-find/client.mjs:1
import { UnionFind } from './unionFind';
         ^^^^^^^^^
SyntaxError: The requested module does not provide an export named 'UnionFind'
    at ModuleJob._instantiate (internal/modules/esm/ModuleJob.js:89:21)
    at <anonymous>

What am I doing wrong here?

5
  • what version of node are you using? Commented Jul 9, 2018 at 17:21
  • 1
    @nijm nope, you can see she's got the experimental modules flag set, which is documented here: nodejs.org/api/esm.html as not needing an extra library if set Commented Jul 9, 2018 at 17:23
  • @Derek version 9.11.1 Commented Jul 9, 2018 at 17:23
  • 2
    seems like you are trying to use named import on your default. Commented Jul 9, 2018 at 17:23
  • @Derek you are right Commented Jul 9, 2018 at 17:25

1 Answer 1

4

In this case, use

       import UnionFind from './UnionFind.js';

if you declared

       export class UnionFind{ ....

then you use

import { UnionFind } from './UnionFind.js';

Also take a look at the file name: UnionFind.js . You are using './unionFind'. This is case sensitive

Sign up to request clarification or add additional context in comments.

2 Comments

Tried both options, still keep getting the same error
Also take a look at the file name: UnionFind.js . You are using './unionFind'. This is case sensitive

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.