如何使用Webpack为Rest API Express NodeJS Server创建包

问题描述 投票:1回答:1

希望你们都很好:)

我正在使用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'));
node.js webpack babeljs webpack-dev-server babel-loader
1个回答
1
投票

由于您将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'
        ]
    }
}
  1. 请注意,我删除了@babel/polyfill,您不需要它用于服务器环境(我确定因为我也将我的服务器代码与webpack捆绑在一起并且从不需要它)。
  2. 确保将node的版本设置为目标版本。
  3. 另请注意,query是将选项传递给webpack加载器的一种非常古老的方式,因此我使用options将其更新为新语法。并且最好通过babel插件的全名,例如:@babel/preset-env而不仅仅是env。解决插件名称的旧方法会生成基于babel-preset-envenv,但是自从babel v7他们将项目重组为“scoped packages”,因此@babel/前缀,所以最好指定全名。
© www.soinside.com 2019 - 2024. All rights reserved.