将JavaScript类导入另一个类时出现意外标识符{classname} [英] Unexpected Identifier {classname} when importing JavaScript Class into another Class

查看:185
本文介绍了将JavaScript类导入另一个类时出现意外标识符{classname}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node v10.11.0并且正在从Ubuntu 18.04运行此脚本。

I'm using Node v10.11.0 and am running this script from Ubuntu 18.04.

我的文件设置如下所示:

My file setup looks like this:

main.js

import Login from './Login.mjs';

class Main {
    constructor() {
        const login = new Login();

        login.login();
    }
}

new Main();

Login.mjs

import readline from 'readline';

class Login {
    constructor() {
        this.username = '';
        this.password = '';
        this.readline = readline.createInterface({
            input: process.stdin,
            output: process.stdout
        });
    }

    login() {
        this.readline.question('What is your username?', answer => {
            this.username = answer;
        });

        this.readline.question('What is your password?', answer => {
            this.password = answer;
        });
    }
}

export default Login;

我正在调用 main.js 使用以下命令:

I'm calling the main.js with the following command:

node --experimental-modules main.js

这会导致以下错误:

(node:7280) ExperimentalWarning: The ESM module loader is experimental.
/home/jrenk/Workspace/bitefight/main.js:1
(function (exports, require, module, __filename, __dirname) { import Login from './Login.mjs';
                                                                 ^^^^^

SyntaxError: Unexpected identifier
    at new Script (vm.js:79:7)
    at createScript (vm.js:251:10)
    at Proxy.runInThisContext (vm.js:303:10)
    at Module._compile (internal/modules/cjs/loader.js:657:28)
    at Object.Module._extensions..js 
    (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at createDynamicModule (internal/modules/esm/translators.js:56:15)
    at setExecutor 
    (internal/modules/esm/create_dynamic_module.js:50:23)

^^^^^ 属于登录但我似乎无法在问题中将其格式化。

The ^^^^^ belongs under the Login but I can't seem to get it formatted right here in the question.

我也试图保存 Login.mjs as Login.js 并调用 main.js 没有 - experimental-modules 但这会导致完全相同的错误。

I also tried to save the Login.mjs as Login.js and calling the main.js without the --experimental-modules but this leads to the exact same error.

这个问题是类似于这个问题。正如我上面所说,我已经尝试了那里所描述但没有运气。

This question is similar to this question. As I said above I already tried what is described there but no luck.

推荐答案

原生ES模块( import export 语句)只能在Node中的.mjs文件中使用。为了使用它们,入口点应命名为 main.mjs

Native ES modules (import and export statements) can only be used in .mjs files in Node. In order to use them, entry point should be named main.mjs.

为了使用ES模块.js文件,ES模块应该被转换为回退到 require ,或者本地使用自定义ES模块加载器。由于后者不是本机Node.js行为,因此不建议将其作为经验法则推荐。

In order to use ES modules in .js files, ES modules should either be transpiled to fall back to require, or used natively with custom ES module loader. Since the latter isn't native Node.js behaviour, it cannot be recommended as a rule of thumb.

这篇关于将JavaScript类导入另一个类时出现意外标识符{classname}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆