简体   繁体   English

为什么Babel的安装在Electron中失败? 我是否需要通天塔?

[英]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 我想使用npm软件包https://www.npmjs.com/package/swipe-detect并收到以下错误消息

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. 我发现错误是因为这是ES6语法。 So I've been trying to install babel according to this tutorial. 因此,我一直在尝试根据本教程安装babel。 https://www.robinwieruch.de/minimal-node-js-babel-setup/ , but I get an error message https://www.robinwieruch.de/minimal-node-js-babel-setup/ ,但出现错误消息

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. 我还尝试了其他一些教程,并在.babelrc中设置环境。 The error message still pops up. 错误消息仍然弹出。

Now I read that babel is already included in Electron. 现在,我了解到Babel已包含在Electron中。 Why is Babel needed in an Electron project . 为什么在电子项目中需要Babel

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. 根据您的Electron / Node版本,使用--experimental-modules参数可能足以使其工作。 (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. 有关在Electron中使用ES模块的一些背景信息,请参见https://github.com/electron/electron/issues/12011 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.) (鉴于您所需的库只有100行 ,并且在一个不错的Liberty MIT许可证下,克隆它,将默认值放在该文件末尾并仅导出该类可能会更容易。)

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

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