首页 > 解决方案 > 带有 Webpack 的 Express JS 找不到模块错误

问题描述

嗨,我是前端技术的新手,所以我对 webpack 和 express js 有疑问。我正在使用 react 、 webpack 、 node 和 express js 构建一个应用程序。app.js 如下所示

应用程序.js

const express = require('express');
    const app = express();
    var path = require('path');
    app.use(express.static(__dirname + '/'));
    app.get('/', (req, res) => { // new
        res.sendFile(__dirname + "/index.html");
    });
    app.listen(3000, () => console.log('listening on port 3000'));

package.json 如下所示

     "version": "1.0.0",
      "main": "app.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 0",
        "build": "webpack --mode production",
        "start": "nohup webpack-dev-server --mode development --open &",
        "prepublishOnly": "cp -r configuration/* build/"
      },
      "repository": {
        "type": "git",
        "url": "ssh://git.amazon.com/pkg/SPSWebApplication"
      },
      "keywords": [],
      "author": "",
      "license": "ISC",
      "devDependencies": {
        "@babel/core": "^7.3.4",
        "@babel/preset-env": "^7.3.4",
        "@babel/preset-react": "^7.0.0",
        "babel-loader": "^8.0.5",
        "express": "^4.16.4",
        "html-loader": "^0.5.5",
        "html-webpack-plugin": "^3.2.0",
        "react-dom": "^16.8.4",
        "webpack": "^4.29.6",
        "webpack-cli": "^3.3.0",
        "webpack-dev-middleware": "^3.6.1",
        "webpack-dev-server": "^3.2.1"
      },
      "dependencies": {
        "@stencil-react/core": "*",
        "@stencil-react/theme-default": "*",
        "webpack": "^4.29.6",
        "webpack-cli": "^3.3.0",
        "react": "^16.8.4",
        "webpack-dev-server": "^3.2.1",
        "html-webpack-plugin": "^3.2.0"
      },
      "description": ""
    }

webpack.config.js

const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
module: {
    rules: [
        {
            test: /\.(js|jsx)$/,
            exclude: /node_modules/,
            use: {
                loader: "babel-loader"
            }

        },
        {
            test: /\.html$/,
            use: [
                {
                    loader: "html-loader",
                    options: { minimize: true }
                }
            ]
        }
    ]
},
resolve: { // this allows to import 'Foo.jsx'
    extensions: ['.jsx', '.js', '.json']
},
plugins: [
    new HtmlWebPackPlugin({
        template: "./src/index.html",
        filename: "./index.html"
    })
]
};

当我在 build 文件夹中构建代码时,它会创建 dist/app.js,main.js,index.html 每当我尝试运行 app.js 时它都会说

Error: Cannot find module 'express'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:649:15)
at Function.Module._load (internal/modules/cjs/loader.js:575:25)
at Module.require (internal/modules/cjs/loader.js:705:19)
at require (internal/modules/cjs/helpers.js:14:16)

我想通过 dist/app.js 在生产环境中运行我的应用程序,但是当我执行“node app.js”时,它给了我这个依赖问题我假设 webpack 应该以缩小的形式在 dist/* 文件夹中提取 express 依赖我假设它没有做。如果我遗漏任何东西并且我的理解正确与否,您能否请教?

标签: node.jsexpresswebpack

解决方案


推荐阅读