Typescript (megajs) bundle webpack работает с ошибкой
Есть такой middleware для express, после сборки в webpack, сервер запускается, но при обращении к странице, получаем ошибку. При чем, если я запускаю «npm run dev» все прекрасно работает. Есть у кого-то догадки как это устранить?
Код Middleware:
import { Response, NextFunction } from 'express';
import * as megajs from 'megajs';
import { FilesCache, Folders, IRequest } from '../types';
var mega: megajs.Storage | null;
var folders: Folders = {};
export var files: FilesCache = {};
export async function storage(req: IRequest, res: Response, next: NextFunction): Promise<void> {
if (mega == null) {
mega = new megajs.Storage({
email: 'тратата',
password: 'тратата',
userAgent: 'SwiftImageManager/0.1',
autoload: true
});
await mega.ready;
}
let folder: string | null = req.params?.folder;
try {
if (typeof folder == 'string' && !(folder in folders)) {
mega.root.children?.forEach((mf: megajs.MutableFile): void => {
if (mf.name == folder) folders[folder as string] = mf;
});
if (!(folder in folders)) {
throw new Error();
} else {
req.mega = mega;
req.folder = folders[folder];
next();
}
} else if (typeof folder != 'string')
throw new Error();
req.mega = mega;
req.folder = folders[folder];
next();
} catch {
res.writeHead(404);
res.end();
}
}
Параметры webpack:
const path = require('path');
module.exports = {
entry: './src/index.ts',
target: 'node', // Укажите цель сборки
mode: 'production',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'swift.image.cluster.js'
},
resolve: {
extensions: ['.ts', '.js']
},
module: {
rules: [
{
test: /\.ts$/,
use: 'ts-loader',
exclude: /node_modules/
}
]
}
};
Package json:
{
"scripts": {
"dev": "npx ts-node src/index.ts",
"start": "node build/swift.image.cluster.js",
"build": "webpack"
},
"dependencies": {
"axios": "^1.1.3",
"body-parser": "^1.20.2",
"cors": "^2.8.5",
"express": "^4.18.2",
"express-fileupload": "^1.4.0",
"md5": "^2.3.0",
"megajs": "^1.1.3",
"nodemon": "^2.0.20",
"zod": "^3.22.4",
"node-fetch": "^3.3.2"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/express": "^4.17.21",
"@types/express-fileupload": "^1.5.0",
"@types/node": "^20.12.3",
"ts-loader": "^9.5.1",
"typescript": "^5.4.3",
"webpack": "^5.91.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
}
}
Ошибка:
TypeError: (0 , Q.default) is not a function
at pe.defaultFetch (D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:177017)
at pe.request (D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:177289)
at Se.login (D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:197037)
at new Se (D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:194848)
at D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:4289
at D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:3911
at Object.next (D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:4016)
at D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:2907
at new Promise (<anonymous>)
at r (D:\Backup\Develop\Node.js\MegaImageServer\build\swift.image.cluster.js:2:2652)
Node.js v20.10.0
UPD как выяснилось (0, Q.default) это:
Object [Module] {
AbortError: [Getter],
FetchError: [Getter],
Headers: [Getter],
Request: [Getter],
Response: [Getter],
default: [Getter]
}
UPDv2: В файле /node_modules/megajs/node_modules/node-fetch/lib/index.mjs
export default fetch;
export { Headers, Request, Response, FetchError, AbortError };
Скорее всего Q.default это попытка вызвать fetch, но почему-то он равен следующему экспорту {Headers, ...} то есть Q выглядит так:
Object [Module] {
AbortError: [Getter],
FetchError: [Getter],
Headers: [Getter],
Request: [Getter],
Response: [Getter],
default: Object [Module] {
AbortError: [Getter],
FetchError: [Getter],
Headers: [Getter],
Request: [Getter],
Response: [Getter],
default: [Getter]
}
}