Meh*_*ein 5 node.js webpack babeljs webpack-dev-server babel-loader
希望你们一切都好:)
我正在使用 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'
]
}
}
Run Code Online (Sandbox Code Playgroud)
.babelrc
{
"presets": ["@babel/env"]
}
Run Code Online (Sandbox Code Playgroud)
包.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"
}
}
Run Code Online (Sandbox Code Playgroud)
我也读过这篇关于在 webpack 中使用 express 的文章,但我无法正确实现它,我认为这是一个原因,它是用于 ReactJS 应用程序的: 如何将 webpack 与 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)
Run Code Online (Sandbox Code Playgroud)
索引.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'));
Run Code Online (Sandbox Code Playgroud)
由于您将 babel 配置直接传递给加载器,因此不需要该.babelrc文件。另外,您使用的是 babel v7,因此下面是更新后的配置(您的 config 和 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'
]
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,我删除了@babel/polyfill,您在服务器环境中不需要它(我确信因为我也将我的服务器代码与 webpack 捆绑在一起并且从未需要它)。
确保将版本设置node为您的目标版本。
query是一种将选项传递给 webpack 加载器的非常古老的方法,因此我将其更新为新语法,使用options. 最好传递 babel 插件的全名,例如:@babel/preset-env而不是仅仅env. 解析插件名称的旧方法会生成一个babel-preset-env基于 的名称env,但从 babel v7 开始,他们将项目重组为“作用域包”,即前缀@babel/,因此最好指定全名。| 归档时间: |
|
| 查看次数: |
5574 次 |
| 最近记录: |