简体   繁体   English

无法使用ES6导入语法

[英]Can't use ES6 import syntax

I'm trying to use the ES6 import syntax and keep running into errors. 我正在尝试使用ES6导入语法,并继续遇到错误。 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 保存在文件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 这保存在文件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? 我在这里做错了什么?

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 . 还要看看文件名:UnionFind.js。 You are using './unionFind'. 您正在使用“ ./unionFind”。 This is case sensitive 这是区分大小写的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM