我正在尝试安装BigCommerce Open Checkout脚本,当我尝试在本地运行基本安装时,我目前遇到了这个错误:
Uncaught TypeError: $ is not a function
at eval (es.array.index-of.js?c975:15)
at Object../node_modules/core-js/modules/es.array.index-of.js
该文件是:
'use strict';
var $ = require('../internals/export');
var $indexOf = require('../internals/array-includes').indexOf;
var arrayMethodIsStrict = require('../internals/array-method-is-strict');
var arrayMethodUsesToLength = require('../internals/array-method-uses-to-length');
var nativeIndexOf = [].indexOf;
var NEGATIVE_ZERO = !!nativeIndexOf && 1 / [1].indexOf(1, -0) < 0;
var STRICT_METHOD = arrayMethodIsStrict('indexOf');
var USES_TO_LENGTH = arrayMethodUsesToLength('indexOf', { ACCESSORS: true, 1: 0 });
// `Array.prototype.indexOf` method
// https://tc39.github.io/ecma262/#sec-array.prototype.indexof
$({ target: 'Array', proto: true, forced: NEGATIVE_ZERO || !STRICT_METHOD || !USES_TO_LENGTH }, {
indexOf: function indexOf(searchElement /* , fromIndex = 0 */) {
return NEGATIVE_ZERO
// convert -0 to +0
? nativeIndexOf.apply(this, arguments) || 0
: $indexOf(this, searchElement, arguments.length > 1 ? arguments[1] : undefined);
}
});
到目前为止,我已经尝试过多次更新core-js和NPM,但都没有成功。
core-js不应该由Babel编译。使用webpack+babel编译代码时,需要确保babel加载程序的webpack模块规则不包括core-js。
选项1
告诉webpack不要对node_modules中的任何依赖项使用babel加载器。由于core-js是node_modules中的一个依赖项,这就排除了babel对core-js的处理。
// webpack.config.js
// This code snippet shows only the relevant part of the webpack config
module.exports = {
module: {
rules: [
{
test: /.m?(j|t)sx?$/,
// Excluding node_modules means that core-js will not be compiled
exclude: /node_modules/,
use: ['babel-loader']
}
]
}
}
选项2
告诉webpack使用babel编译所有依赖项,除了核心js依赖项:
// webpack.config.js
// This code snippet shows only the relevant part of the webpack config
module.exports = {
module: {
rules: [
{
test: /.m?(j|t)sx?$/,
// Compile all node_modules except core-js
include: {
and: [/node_modules/],
not: [/core-js/]
},
use: ['babel-loader']
}
]
}
}
相关链接
- https://github.com/zloirock/core-js/issues/912
- https://webpack.js.org/configuration/module/#ruleexclude
- https://webpack.js.org/configuration/module/#ruleinclude
- https://webpack.js.org/configuration/module/#rule-条件