當我嘗試在帶有 Webpack 5 的專案中使用keyStoresfrom時出現運行時錯誤。near-api-js
在 index.js 中
const keyStore = new keyStores.BrowserLocalStorageKeyStore();
錯誤資訊:
bundle.js:1549 Uncaught TypeError: str.split is not a function
at toIdentifier (index.js:26:6)
at forEachCode (index.js:267:16)
at Array.forEach (<anonymous>)
at populateConstructorExports (index.js:265:9)
at eval (index.js:31:1)
at Object../node_modules/http-errors/index.js (bundle.js:497:1)
at __webpack_require__ (bundle.js:1546:33)
at fn (bundle.js:1763:21)
at eval (web.js:7:39)
at Object../node_modules/near-api-js/lib/utils/web.js (bundle.js:965:1)
我懷疑錯誤出在 webpack 5 中,這是我的 webpack.config.js 檔案
const path = require('path');
const webpack = require('webpack');
const NodePolyfillPlugin = require('node-polyfill-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
module.exports = (env, argv) => {
console.log('env', env);
console.log('argv', argv);
return {
entry: {
main: './src/index.js',
},
optimization: {
splitChunks: {
cacheGroups: {
phaser: {
test: /[\\/]node_modules[\\/]phaser[\\/]/,
name: 'phaser',
chunks: 'all',
},
},
},
},
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist'),
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
{
test: /\.json/,
type: 'asset/resource',
},
],
},
resolve: {
extensions: ['.tsx', '.ts', '.js'],
},
devServer: {
port: 9090,
},
plugins: [
new HtmlWebpackPlugin({
template: path.join(__dirname, 'src/index.html'),
}),
new NodePolyfillPlugin(),
new webpack.DefinePlugin({
'process.env': JSON.stringify({ mode: argv.mode }),
}),
],
};
};
我在這里創建了一個簡約的例子https://stackblitz.com/edit/github-ytsewr-hnvwjf
uj5u.com熱心網友回復:
經過多次嘗試,我想我找到了解決方案。
我必須添加
exclude: /node_modules/,測驗規則/\.json/,:
module: {
rules: [
{
test: /\.json/,
type: 'asset/resource',
exclude: /node_modules/, // <-- add this line
},
],
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/482258.html
