关于用于外部库和nodejs的webpack docs,有这样的信息,即应该能够导入节点模块。除非我要导入 http2 ,否则这将非常有效。它显示以下错误:
ERROR in ./src/index.ts
Module not found: Error: Can't resolve 'http2' in 'absolutepath/to/src'
简化的条目打字稿文件如下:
import { readFileSync } from 'fs';
import { createSecureServer } from 'http2';
const server = createSecureServer({...});
webpack配置文件如下所示:
const path = require('path');
module.exports = {
entry: './src/index.ts',
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'index.js'
},
node: {
global: true,
process: true,
fs: 'empty',
http2: 'empty'
},
module: {
rules: [
{ test: /\.ts$/, use: 'ts-loader' }
]
},
resolve: {
extensions: [".ts", ".tsx", ".js"],
modules: [path.resolve(__dirname, '/src'), 'node_modules/'],
descriptionFiles: ['package.json']
},
mode: 'production'
}
还有tsconfig.json
{
"compileOnSave": false,
"compilerOptions": {
"experimentalDecorators": true,
"target": "es5",
"module": "commonjs",
"emitDecoratorMetadata": true,
"outDir": "dist",
"declaration": false,
"sourceMap": true,
"removeComments": true,
"moduleResolution": "node",
"strict": true,
"noImplicitAny": false,
"typeRoots": [
"node_modules/@types"
],
"lib": [
"es2017"
]
}
}
例如导入 fs 时不会出错。我还看到了node-libs-browser插件,其中未列出http2模块。但是我不想在浏览器中使用http2,而只是为了捆绑我的后端应用程序。有什么建议或想法吗? 谢谢,加油!