node.js - 带有 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 依赖我假设它没有做。如果我遗漏任何东西并且我的理解正确与否,您能否请教?
解决方案
推荐阅读
- git - Git checkout 将文件传输到另一个分支
- reactjs - NextJS Button 通过 React 的 useState 不断单击自身
- laravel - Laravel:为列的值设置别名
- javascript - Javascript代码在Firefox浏览器中不起作用
- unreal-engine4 - 在虚幻引擎上具有同步延迟的游戏 CPU 和 GPU
- amazon-web-services - Jenkins 代理 EC2 实例创建失败并出现 404 NOT FOUND 错误
- flutter - [已解决]转换未来
在 Flutter 构建方法中将变量转换为普通字符串 - node.js - Sequelize NodeJS 服务器抛出“ERR_UNKNOWN_ENCODING”错误
- fastapi - ImportError:无法从“fastapi”导入名称“请求”
- xamarin.forms - 如何使用 Xamarin Forms 识别是否在 iOS 中启用了自动日期和时间