gpt4 book ai didi

node.js - 如何使用 Webpack 为 Rest API Express NodeJS 服务器创建包

转载 作者:搜寻专家 更新时间:2023-10-31 23:56:42 26 4
gpt4 key购买 nike

希望你一切都好:)

我当时正在使用 Nodejs 开发 REST API 服务器,在完成 alpha 版本后,我想用构建工具为其创建一个包,虽然我在某些时候成功地捆绑了应用程序,但仍然无法制作 Express Rest API要捆绑的脚本。由于我对 Webpack 没有真正的经验,我很确定我做错了什么,必须有办法做到这一点。您还可以在下面看到我的 webpack.config.js、.babelrc 和 package.json:

Webpack.config.js

var debug = process.env.NODE_ENV !== "production";
var webpack = require('webpack');
var module_dir = `${__dirname}/node_modules`;
const path = require('path');

module.exports = {
entry: {
app: [
'babel-polyfill',
'./index.js',
],
},
output: {
path: path.resolve(__dirname, 'build'),
filename: 'app.bundle.js',
},
module: {
rules: [{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['env', 'stage-0']
}
}]
},
resolveLoader: {
modules: [
__dirname + '/node_modules'
]
}
}

.Babelrc

{
"presets": ["@babel/env"]
}

package.json

{
"name": "",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "nodemon index.js",
"build": "webpack --mode production --progress"
},
"keywords": [
"log",
"npm",
"node",
"rest",
"api",
"debug",
"bug"
],
"author": "Mehdi Roshan Fekr",
"license": "ISC",
"dependencies": {
"@babel/core": "^7.3.4",
"@babel/preset-env": "^7.3.4",
"express": "^4.16.4",
"joi": "^14.3.1",
"nodemon": "^1.18.10"
},
"devDependencies": {
"@babel/core": "^7.1.6",
"@babel/preset-env": "^7.1.6",
"@babel/preset-react": "^7.0.0",
"babel-loader": "^8.0.4",
"webpack": "^4.25.1",
"webpack-cli": "^3.1.2"
}
}

我也读过这篇关于在 webpack 中使用 express 的文章,但我无法正确实现它,我认为这是一个原因,它是针对 ReactJS 应用程序的: How can I use webpack with express?

----更新-----

错误

ERROR in ./index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Cannot find module 'babel-preset-env' from 'C:\Projects\# App Projects\Qcentic-Log'
- Did you mean "@babel/env"?
at Function.module.exports [as sync] (C:\Projects\# App Projects\Qcentic-Log\node_modules\resolve\lib\sync.js:58:15)
at resolveStandardizedName (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\files\plugins.js:101:31)
at resolvePreset (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\files\plugins.js:58:10)
at loadPreset (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\files\plugins.js:77:20)
at createDescriptor (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\config-descriptors.js:154:9)
at items.map (C:\Projects\# App Projects\Qcentic-Log\node_modules\@babel\core\lib\config\config-descriptors.js:109:50)

index.js

const express = require('express');
const app = express();
const CustomModule = require('./CustomModule');
app.use(express.json());
//My Endpoints...
app.listen(80, () => console.log('Listening on port 80'));

最佳答案

由于您将 babel 配置直接传递给加载程序,因此不需要 .babelrc 文件。此外,您使用的是 babel v7,所以下面是更新后的配置(您的配置和 package.json 包含 babel v6 和 v7 的混合包,它们不能一起工作):

module.exports = {
target: "node",
entry: './index.js',
output: {
path: path.resolve(__dirname, 'build'),
filename: 'app.bundle.js',
},
module: {
rules: [{
test: /\.js?$/,
exclude: /node_modules/,
loader: 'babel-loader',
options: {
presets: [
[
"@babel/preset-env",
{
targets: {
node: "8.10"
}
}
]
]
}
}]
},
resolveLoader: {
modules: [
__dirname + '/node_modules'
]
}
}
  1. 请注意,我删除了 @babel/polyfill,服务器环境不需要它(我敢肯定,因为我还将我的服务器代码与 webpack 捆绑在一起,并且从不需要它).

  2. 确保将 node 的版本设置为您的目标版本。

  3. 另请注意,query 是一种将选项传递给 webpack 加载程序的非常古老的方法,因此我使用 options 将其更新为新语法。最好传递 babel 插件的全名,例如:@babel/preset-env 而不仅仅是 env。解析插件名称的旧方法会生成一个基于 envbabel-preset-env,但自 babel v7 以来,他们将项目重组为“范围包”,因此 @babel/前缀,最好写全名。

关于node.js - 如何使用 Webpack 为 Rest API Express NodeJS 服务器创建包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55137261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com