简体   繁体   中英

Why is the installation of Babel failing in Electron? And do I need Babel or not?

I want to use the npm package https://www.npmjs.com/package/swipe-detect and get the following error message

export default function(target, callback, threshold=150) {
^^^^^^

SyntaxError: Unexpected token export
    at Module._compile (internal/modules/cjs/loader.js:776:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:851:10)
    at Module.load (internal/modules/cjs/loader.js:701:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:633:12)
    at Function.Module._load (internal/modules/cjs/loader.js:625:3)
    at Module.require (internal/modules/cjs/loader.js:739:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (C:\Users\folder\main.js:22:22)
    at Module._compile (internal/modules/cjs/loader.js:839:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:851:10)

I found out that the error is because this is the ES6 syntax. So I've been trying to install babel according to this tutorial. https://www.robinwieruch.de/minimal-node-js-babel-setup/ , but I get an error message

npm WARN deprecated @babel/polyfill@7.4.4: 🚨 As of Babel 7.4.0, this
npm WARN deprecated package has been deprecated in favor of directly
npm WARN deprecated including core-js/stable (to polyfill ECMAScript
npm WARN deprecated features) and regenerator-runtime/runtime
npm WARN deprecated (needed to use transpiled generator functions):
npm WARN deprecated
npm WARN deprecated   > import "core-js/stable";
npm WARN deprecated   > import "regenerator-runtime/runtime";

I also tried several other tutorials and set the environment in .babelrc. The error message still pops up.

Now I read that babel is already included in Electron. Why is Babel needed in an Electron project .

I've been searching for two days now and can't find a solution. Why does this error pop up?

Depending on your Electron/Node version, using the --experimental-modules argument may be enough to get it working. (However there may be issues when you try to package your app .)

See https://github.com/electron/electron/issues/12011 for some background on using ES modules in Electron. There appear to be various workarounds suggested in that thread.

(Given that the library you want is only 100 lines , and under a nice liberal MIT license, it might be easier to just clone it, drop the default at the end of that file, and simply export the class.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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