我面临着一个经常在不同地方讨论的特定问题,我稍后将分享该链接。该问题与 webpack 捆绑有关。我的 nodeJS 应用程序使用 @azure/storage-blob 库来获取数据,虽然在未捆绑时它工作得很好,但一旦我使用 webpack 捆绑应用程序,应用程序就会开始抛出以下错误:
堆栈跟踪:类型错误:预期信号是新请求 (webpack://ECPNodeAPI/./node_modules/node-fetch/lib/index.mjs?:1229:10) 处 eval ( webpack://ECPNodeAPI/./node_modules/node-fetch/lib/index.mjs?:1416:19) 在新的 Promise () 处获取 (webpack://ECPNodeAPI/./node_modules/node-fetch/lib/index .mjs?:1414:9) 在 NodeFetchHttpClient2.eval (webpack://ECPNodeAPI/./node_modules/@azure/core-http/es/src/nodeFetchHttpClient.js?:69:74) 在步骤 (webpack://ECPNodeAPI/./node_modules/@azure/core-http/node_modules/tslib/tslib.es6.js?:195:19) 在 Object.eval [如下] (webpack://ECPNodeAPI/./node_modules/@azure/core-http/node_modules/tslib/tslib.es6.js?:142:14) 在 eval (webpack://ECPNodeAPI/./node_modules/@azure/core-http/node_modules/tslib/tslib.es6.js?: 128:67) 在 __awaiter 处的 new Promise () (webpack://ECPNodeAPI/./node_modules/@azure/core-http/node_modules/tslib/tslib.es6.js?:110:10)
Node 获取问题中提到了该问题:https://github.com/node-fetch/node-fetch/issues/784 。虽然最新版本声称已修复此问题。但@azure/storage-blob正在使用旧版本的node-fetch。
我尝试过但没有成功的解决方案。
- 使用 yarn 分辨率能够使用最新版本的node-fetch然后捆绑。试过了没用。
- 通过将最小化设置为 false 并使用 terser 插件。我尝试了各种组合,但没有像这里这样的运气 https://github.com/Azure/ms-rest-nodeauth/issues/83#issuecomment-690927491
其他人还有其他解决方法吗?
最佳答案
我能够使用 Terser 插件解决这个问题。这是可能帮助您解决问题的 webpack.config.js 文件。
import path from 'path';
import TerserPlugin from 'terser-webpack-plugin';
import nodeExternals from 'webpack-node-externals';
const __dirname = path.dirname('.');
const isProd = true; // process.env.NODE_ENV === 'production';
export default {
mode: isProd ? 'production' : 'development',
target: 'node',
externals: [nodeExternals()],
entry: './index.mjs',
resolve: {
extensions: ['.js', '.ts'],
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'api.bundle.js',
clean: true,
},
optimization: {
minimize: false,
minimizer: [
new TerserPlugin({
terserOptions: {
keep_fnames: /AbortSignal/,
sourceMap: true,
},
}),
],
},
module: {
rules: [{
test: /\.html$/,
use: 'raw-loader'
},
{
test: /\.js$/,
loader: 'esbuild-loader',
options: {
// loader: 'jsx', // Remove this if you're not using JSX
target: 'es2018', // Syntax to compile to (see options below for possible values)
},
},
{
test: /\.tsx?$/,
loader: 'esbuild-loader',
options: {
loader: 'ts', // Or 'ts' if you don't need tsx
target: 'es2015',
},
},
],
},
};
关于node.js - Webpack @azure/storage-blob node-fetch AbortSignal 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70332883/