[Fixed] How to build express use webpack?

Issue

I am a beginner, just started to learn express and webpack, I tried to use the initial express file
Trying to use webpack for packaging, I keep getting errors

The reason for this is because I want to run my system in a non-node.js environment

Is my direction wrong?

The express project is create with express --view=ejs myapp
I didn’t make any changes

webpack.config.js

const path = require('path');
const clientConfig = {
    resolve: {

        fallback: {
            "fs": false,
            "tls": false,
            "net": false,
            "path": false,
            "zlib": false,
            "http": false,
            "https": false,
            "stream": false,
            "crypto": false,
            "crypto-browserify": require.resolve('crypto-browserify'), //if you want to use this module also don't forget npm i crypto-browserify 
        }
    },
    entry: {
        'index': './app.js'
    },
    output: {
        path: path.join(__dirname, 'dist'),
      
        filename: '[name].bundle.js'
    }
}
module.exports = [clientConfig];

package.json

{
  "name": "myapp",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www",
    "webpack": "webpack"
  },
  "browser": {
    "crypto": false
  },
  "dependencies": {
    "cookie-parser": "~1.4.4",
    "crypto-browserify": "^3.12.0",
    "debug": "~2.6.9",
    "express": "~4.16.1",
    "http-errors": "~1.6.3",
    "morgan": "~1.9.1",
    "path": "^0.12.7",
    "twig": "~0.10.3"
  },
  "main": "app.js",
  "devDependencies": {
    "webpack": "^5.26.3",
    "webpack-cli": "^4.5.0"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": ""
}

Thanks for any guidance and comments

Solution

You can’t run Express in a non-nodejs environment. Express depends upon the nodejs run-time library such as the http module so it can’t run without that library. webpack can’t overcome that.

You can use webpack to build either plain Javascript with no external dependencies or with dependencies that you have independent libraries for that can be included in the webpack bundle or with dependences that are present in whatever environment you want to run it in.

Leave a Reply

(*) Required, Your email will not be published