node.js - 使用 Webpack 捆绑 Sqlite3 导致编译错误
问题描述
我刚刚将 Sqlite3 作为依赖项添加到我的项目(在服务器端),当我尝试捆绑服务器代码以在生产中部署时,webpack 现在会产生一些错误。
WARNING in ./node_modules/node-pre-gyp/lib/util/compile.js 39:27-49
Module not found: Error: Can't resolve 'npm' in 'C:\git\server\node_modules\node-pre-gyp\lib\util'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/compile.js ./util/compile
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
ERROR in ./node_modules/node-gyp/lib/Find-VS2017.cs 7:6
Module parse failed: Unexpected token (7:6)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
| // Usage:
| // powershell -ExecutionPolicy Unrestricted -Version "2.0" -Command "&{Add-Type -Path Find-VS2017.cs; [VisualStudioConfiguration.Main]::Query()}"
> using System;
| using System.Text;
| using System.Runtime.InteropServices;
@ ./node_modules/node-gyp/lib/ sync ^\.\/.*$ ./Find-VS2017.cs
@ ./node_modules/node-gyp/lib/node-gyp.js 55:13-36 197:38-55
@ ./node_modules/node-pre-gyp/lib/util/compile.js 29:28-55
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/compile.js ./util/compile
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
ERROR in ./node_modules/node-gyp/lib/node-gyp.js 67:16-37
Module not found: Error: Can't resolve '../package' in 'C:\git\server\node_modules\node-gyp\lib'
@ ./node_modules/node-pre-gyp/lib/util/compile.js 29:28-55
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/compile.js ./util/compile
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
ERROR in ./node_modules/node-pre-gyp/lib/info.js 14:14-32
Module not found: Error: Can't resolve 'aws-sdk' in 'C:\git\server\node_modules\node-pre-gyp\lib'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./info.js ./info
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
ERROR in ./node_modules/node-pre-gyp/lib/publish.js 18:14-32
Module not found: Error: Can't resolve 'aws-sdk' in 'C:\git\server\node_modules\node-pre-gyp\lib'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./publish.js ./publish
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
ERROR in ./node_modules/node-pre-gyp/lib/unpublish.js 16:14-32
Module not found: Error: Can't resolve 'aws-sdk' in 'C:\git\server\node_modules\node-pre-gyp\lib'
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./unpublish.js ./unpublish
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
ERROR in ./node_modules/node-pre-gyp/lib/util/nw-pre-gyp/index.html 1:0
Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>
@ ./node_modules/node-pre-gyp/lib/ sync ^\.\/.*$ ./util/nw-pre-gyp/index.html
@ ./node_modules/node-pre-gyp/lib/node-pre-gyp.js 52:13-36 184:38-55
@ ./node_modules/sqlite3/lib/sqlite3-binding.js 1:13-36
@ ./node_modules/sqlite3/lib/sqlite3.js 2:14-45
@ ./src/persistence/SqlLiteDatabase.ts 8:18-36
@ ./src/messaging/ObjectService.ts 13:26-67
@ ./src/main.ts 41:24-60
webpack 5.10.0 compiled with 6 errors and 1 warning in 20200 ms
Webpack 配置如下所示:
const path = require("path");
const webpack = require('webpack');
const { CleanWebpackPlugin } = require('clean-webpack-plugin');
const DotEnvPlugin = require('dotenv-webpack');
const WebPackIgnorePlugin =
{
checkResource: function(resource)
{
const lazyImports =
[
'@nestjs/microservices',
'@nestjs/microservices/microservices-module',
'cache-manager',
'class-transformer',
'class-validator',
'fastify-static',
];
if (!lazyImports.includes(resource))
return false;
try
{
require.resolve(resource);
}
catch (err)
{
return true;
}
return false;
}
};
module.exports =
{
mode: 'production',
target: 'node',
entry:
{
server: './src/main.ts',
},
devtool: 'source-map',
module:
{
exprContextCritical: false,
rules:
[
{
test: /\.tsx?$/,
use: 'ts-loader',
exclude: /node_modules/,
},
],
},
resolve:
{
extensions: [ '.tsx', '.ts', '.js' ],
},
node: {
__dirname: false,
},
plugins:
[
new CleanWebpackPlugin(),
new webpack.IgnorePlugin(WebPackIgnorePlugin),
new DotEnvPlugin({ path: './.env', safe: true }),
new webpack.DefinePlugin({ 'process.env.NODE_ENV': '"production"' }),
],
optimization:
{
minimize: false
},
performance:
{
maxEntrypointSize: 10 * 1024 * 1024,
maxAssetSize: 10 * 1024 * 1024
},
output:
{
filename: '[name].js',
path: path.resolve(__dirname, 'prod'),
},
};
配置:
{
"compilerOptions": {
"incremental": true, /* Enable incremental compilation */
"target": "ES2020", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019', 'ES2020', or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', 'es2020', or 'ESNext'. */
"sourceMap": true, /* Generates corresponding '.map' file. */
"outDir": "./prod", /* Redirect output structure to the directory. */
"strict": true, /* Enable all strict type-checking options. */
"baseUrl": "./", /* Base directory to resolve non-absolute module names. */
"allowSyntheticDefaultImports": true, /* Allow default imports from modules with no default export. This does not affect code emit, just typechecking. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"experimentalDecorators": true, /* Enables experimental support for ES7 decorators. */
"emitDecoratorMetadata": true, /* Enables experimental support for emitting type metadata for decorators. */
"skipLibCheck": true, /* Skip type checking of declaration files. */
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
"resolveJsonModule": true /* Allow importing data from json files. */
}
}
包.json:
{
"name": "myproject",
"version": "1.0.0",
"description": "",
"author": "",
"private": true,
"license": "ISC",
"scripts": {
"build": "webpack"
},
"dependencies": {
"@nestjs/common": "^7.4.2",
"@nestjs/core": "^7.4.2",
"@nestjs/passport": "^7.1.0",
"@nestjs/platform-express": "^7.4.2",
"@nestjs/platform-ws": "^7.4.2",
"@nestjs/serve-static": "^2.1.3",
"@nestjs/websockets": "^7.4.2",
"client": "file:../client",
"common": "file:../common",
"cors": "^2.8.5",
"dotenv": "^8.2.0",
"encoding-japanese": "^1.0.30",
"express-session": "^1.17.1",
"passport": "^0.4.1",
"passport-custom": "^1.1.1",
"reflect-metadata": "^0.1.13",
"sqlite3": "^5.0.0",
"winston": "^3.3.3",
"winston-daily-rotate-file": "^4.5.0",
"ws": "^7.3.1"
},
"devDependencies": {
"@nestjs/cli": "^7.4.1",
"@nestjs/schematics": "^7.0.1",
"@nestjs/testing": "^7.4.2",
"@types/cors": "^2.8.6",
"@types/encoding-japanese": "^1.0.16",
"@types/express": "^4.17.6",
"@types/express-session": "^1.17.0",
"@types/jest": "^26.0.0",
"@types/node": "^14.0.13",
"@types/passport": "^1.0.4",
"@types/sqlite3": "^3.1.6",
"@types/supertest": "^2.0.8",
"@types/ws": "^7.2.6",
"@typescript-eslint/eslint-plugin": "3.0.2",
"@typescript-eslint/parser": "3.0.2",
"bufferutil": "^4.0.1",
"clean-webpack-plugin": "^3.0.0",
"copyfiles": "^2.3.0",
"cross-env": "^7.0.2",
"dotenv-webpack": "^2.0.0",
"eslint": "^7.3.0",
"eslint-config-prettier": "^6.10.0",
"eslint-plugin-import": "^2.20.1",
"jest": "26.0.1",
"json-d-ts": "^1.0.1",
"prettier": "^1.19.1",
"supertest": "^4.0.2",
"ts-jest": "26.1.0",
"ts-loader": "^6.2.2",
"ts-node": "^8.10.2",
"typescript": "^4.0.2",
"utf-8-validate": "^5.0.2",
"webpack": "^5.3.2",
"webpack-cli": "^4.1.0"
}
}
解决方案
推荐阅读
- node.js - Node.js 缺少 POST 请求的主体
- node.js - 是否可以访问 DynamoDB ExpressionAttributeNames 中的对象字段?
- json - 将 JSON 数据添加到 React
- heroku - 我可以得到我的 heroku 应用程序的确切区域吗?
- arrays - 指向节点指针数组的节点指针
- arrays - Mongodb 项目只键入一个对象
- java - 如何在Java中扩展一个数字以匹配另一个数字
- javascript - NextJS _app.tsx Component 和 pageProps 应该是什么 TypeScript 类型?
- javascript - javascript:如何将数组转换为元组
- javascript - 我的按钮标签对我的 JavaScript RoR 6 没有反应