嗨,我无法让SerialPort在电子设备上工作。 而且我不知道这是否是webpack打字稿或其他电子错误。 我在webpack中尝试了一些不同的设置,但从未使它始终与相同的错误一起工作。
我的webpack.config.js
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');
const commonConfig = {
output: {
path: path.resolve(__dirname, 'dist'),
filename: '[name].js'
},
node: {
__dirname: false,
},
watchOptions: {
poll: 100
},
mode: 'development',
module: {
rules: [
{
test: /\.ts?$/,
loader: 'ts-loader'
}
]
},
resolve: {
extensions: ['.js', '.ts', '.json']
},
plugins: [new CopyWebpackPlugin([
{ from: './src/*.html', to: './[name].html' },
{ from: './src/*.css', to: './[name].css' },
], {
flatten: true
})]
};
module.exports = [
Object.assign(
{
target: 'electron-main',
entry: {main: './src/main.ts'}
},
commonConfig),
Object.assign(
{
target: 'electron-renderer',
entry: {gui: './src/gui.ts'},
plugins: [new HtmlWebpackPlugin()]
},
commonConfig)
];
我的gui.ts
import * as SerialPort from "serialport";
SerialPort.list().then(ports => {
console.log(ports);
});
并且电子中的误差为
Uncaught ReferenceError: require is not defined
at eval (external_"stream":1)
at Object.stream (gui.js:471)
at __webpack_require__ (gui.js:20)
at eval (stream.js:1)
at Object../node_modules/@serialport/stream/stream.js (gui.js:349)
at __webpack_require__ (gui.js:20)
at eval (index.js:1)
at Object../node_modules/serialport/lib/index.js (gui.js:393)
at __webpack_require__ (gui.js:20)
at eval (gui.ts:2)