首页 > 解决方案 > 使用 'const' 关键字会在 babel 中触发错误

问题描述

我将 babel / browserify 与 gulp 和 composer 一起使用。

当我使用 const 时,它会在 gulp 中触发以下错误

(node:15308) UnhandledPromiseRejectionWarning: TypeError: C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\src\js\form.js: Cannot read property 'bindings' of null while parsing file: C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\src\js\form.js
    at Scope.moveBindingTo (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\scope\index.js:926:13)
    at convertBlockScopedToVar (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\babel-plugin-transform-es2015-block-scoping\lib\index.js:139:13)
    at PluginPass.VariableDeclaration (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\babel-plugin-transform-es2015-block-scoping\lib\index.js:26:9)
    at newFn (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\visitors.js:179:21)
    at NodePath._call (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\path\context.js:55:20)
    at NodePath.call (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\path\context.js:42:17)
    at NodePath.visit (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\path\context.js:90:31)
    at TraversalContext.visitQueue (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\context.js:112:16)
    at TraversalContext.visitMultiple (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\context.js:79:17)
    at TraversalContext.visit (C:\xampp\htdocs\plugin\wp-content\plugins\nsp-adhesion\node_modules\@babel\traverse\lib\context.js:138:19)
(node:15308) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:15308) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

仅当它被包裹时才有效

document.addEventListener('DOMContentLoaded', function(e) {
   // global variables
   const test = "test";    
});

这是正常行为吗?

标签: javascriptecmascript-6babeljsbrowserify

解决方案


在这篇文章中找到了答案,

是 6 和 7 之间的 babel 依赖不匹配

尝试转译时 JavaScript babel“TypeError:无法读取 null 的属性‘绑定’”


推荐阅读