A
A
Anton Boyarshinov2018-09-22 20:47:34
JavaScript
Anton Boyarshinov, 2018-09-22 20:47:34

Import in nodejs 10.11.0 - how to make it work?

import app from 'electron';
import BrowserWindow from 'electron';

app.on('ready', () => {

   createWindow();

});

app.on('window-all-closed', function() {
    app.quit();
});


function createWindow() {
    let appWindow = new BrowserWindow({
        width: 800,
        height: 600,
        devTools:true,
    });
    appWindow.loadURL(path.join(__dirname, 'App/index.html'));
    return appWindow;
};

When run, it gives :
import app from 'electron';
^^^
SyntaxError: Unexpected identifier
If replaced with import {app,BrowserWindow} from 'electron'; gives:
import {app,BrowserWindow} from 'electron';
^
SyntaxError: Unexpected token {
I run it like this:
"start": "node app.js"
with the --experimental-modules flag, I also tried it - nothing changes
Any idea what the problem is?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
K
Kirill Mokevnin, 2018-09-22
@toxicmt

It's best to use https://babeljs.io/ and not worry about what is for the backend and what is for the frontend. Firstly, you don’t have to change anything (such as an extension for using mjs), on the other hand, now it is the de facto standard in the js world, and thirdly, there is no way at all in the front without it.

S
Sergey delphinpro, 2018-09-22
@delphinpro

https://stackoverflow.com/questions/45854169/how-c...

F
Flying, 2018-09-22
@Flying

As stated in the documentation - ES6 imports only work in files with the .mjs extension when the --experimental-modules flag is enabled.
The reasons for this behavior are well written in an article from one of the node.js developers

A
Abcdefgk, 2018-09-23
@Abcdefgk

And if such an unexpected option.
Write, for example, like this May work (but this is not accurate).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question