Webpack packages the project using the redis Library of nodejs, and the packaged JS can be used in the browser

CSDN Q & A 2022-02-13 05:22:31 阅读数:642

webpack packages project using redis

Namely Webpack When packing, put some NodeJs The core library is also packaged into the generated js in .
Webpack The official description can do this :Webpack Chinese document /node
Officials say it can be in webpack.config.js Add

// The official sample node: { console: false, global: true, process: true, __filename: "mock", __dirname: "mock", Buffer: true, setImmediate: true //  More options , Please check out “ other  Node.js  Core library ”}

These options allow you to configure whether polyfill or mock some Node.js Global variables and modules . This makes it possible to start with Node.js Code written in the environment , In other environments ( Such as browser ) Run in .

But in fact, at present Webpack5.42.0 Only

// my webpack.config.js in node: {
 global: true, __dirname: "mock", __filename: "mock",}

When I add... To my code above

assert:true,util:true,net:"mock",tls:"mock",string_decoder:true,Buffer:true

when WebStorm Tips webpack: Property 'Buffer' is not allowed ( and Buffer Similarly, other attributes also report errors ).
But without these , In execution webpack --config webpack.config.js Report a mistake later :

ERROR in ./node_modules/redis-errors/lib/modern.js 3:15-32
Module not found: Error: Can't resolve 'assert' in 'D:...( My project path )\node_modules\redis-errors\lib'
BREAKING CHANGE: webpack < 5 used to include polyfills for node.js core modules by default.
This is no longer the case. Verify if you need this module and configure a polyfill for it.
If you want to include a polyfill, you need to:
- add a fallback 'resolve.fallback: { "assert": require.resolve("assert/") }'
- install 'assert'
If you don't want to include a polyfill, you can use an empty module like this:
resolve.fallback: { "assert": false }

For the first time to use Webpack, even to the extent that NodeJS It only took a week , If it is Webpack For the version problem, please ask me how to operate after changing to a lower version ...... Or how to solve the problem in the current version
I just want one NodeJs Of redis After the client is packaged, it just runs on the web page
Here is my complete webpack.config.js Code

const webpack=require('webpack')const path = require('path')function resolve(relatedPath) { return path.join(__dirname, relatedPath)}module.exports = { entry: { index: resolve('../untitled1/index.js') }, output: { path: resolve('../untitled1/dist'), filename: 'MyRedisClient.js', }, target: "web", node:{ global: true, __dirname: "mock", __filename: "mock", }, mode:"development"}



Take the answer :

Hello , I am a little assistant who answers every question , So sorry , This time, you have to answer all your questions , The panel of technical experts timed out and did not answer for you

The number of times to answer all questions deducted from this question , Will answer with questions VIP Experience card (1 A chance to answer every question 、 Shopping malls buy physical books and enjoy 95 A discount ) It will be reissued to your account in the form of .

​​​​ Because all questions must be answered VIP The experience card is only valid for 1 God , When you need it 【 Direct messages 】 Contact me , I'll reissue it for you .


copyright:author[CSDN Q & A],Please bring the original link to reprint, thank you. https://en.javamana.com/2022/02/202202130522289518.html