一、使用webpack打包nodejs 后台端环境
第一步、安装webpack包,初始化项目应用
npm install webpack webpack-cli --save-dev
第二步、修改编译配置
1.增加webpack.config.js文件,配置输入输出,重点制定target 运行环境为node
const path = require('path');
module.exports = {
entry: './src/main.js',
output: {
filename: 'bundle.js',
path: path.resolve(__dirname, 'dist')
},
target: 'node' // 这是最关键的
};
2.修改 package.json 的编译命令: "build":"webpack"
{
"name": "Test4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"dependencies": {
"webpack": "^5.3.2",
"webpack-cli": "^4.1.0"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build":"webpack"
},
"keywords": [],
"author": "",
"license": "ISC"
}
第三步、编写代码,并编译发布
npm run build
运行效果:
编译后代码: