【发布时间】:2021-09-11 07:44:55
【问题描述】:
我正在使用 dot-env NPM 包将简单的变量传递给我的 webpack/express 应用程序。
当我在 webpack 的 PRODUCTION 模式下运行时,我来自 .env 的所有变量都未定义。
我正在构建一个开发和生产 webpack 配置文件,目前有以下设置。
任何关于我所犯的错误以及为什么我的 .env 变量被删除的建议将不胜感激。
-
package.json(脚本)
"buildDev": "rm -rf dist && webpack --mode development --config webpack.server.config.js && webpack --mode development --config webpack.dev.config.js", "buildProd": "rm -rf dist && webpack --mode production --config webpack.server.config.js && webpack --mode production --config webpack.prod.config.js", -
webpack.dev
const path = require("path"); const webpack = require("webpack"); const HtmlWebPackPlugin = require("html-webpack-plugin"); const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const paths = require("./config/paths"); const isDevelopment = false; const Dotenv = require('dotenv-webpack'); require('dotenv').config() module.exports = { entry: { main: [ "webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000", paths.appIndexJs, ], }, output: { path: path.join(__dirname, "dist"), publicPath: "/", filename: "[name].js", }, devServer: { historyApiFallback: true, }, mode: "production", target: "web", devtool: "#source-map", module: { rules: [ { enforce: "pre", test: /\.(js|jsx)$/, exclude: /node_modules/, loader: "eslint-loader", options: { emitWarning: true, failOnError: false, failOnWarning: false, }, }, { test: /\.(js|jsx)$/, exclude: /node_modules/, include: path.resolve(paths.appSrc), loader: "babel-loader", }, { // Loads the javacript into html template provided. // Entry point is set below in HtmlWebPackPlugin in Plugins test: /\.html$/, include: path.resolve(paths.appSrc), use: [ { loader: "html-loader", }, ], }, { test: /\.css$/, include: path.resolve(paths.appSrc), use: ["style-loader", "css-loader"], }, { test: /\.css$/, include: /node_modules/, use: ["style-loader", "css-loader"], }, { test: /\.scss$/, use: ["style-loader", "css-loader", "sass-loader"], }, { test: /\.(png|svg|jpg|gif|eot|woff|woff2|ttf)$/, use: ["file-loader"], }, ], }, resolve: { extensions: [".js", ".jsx"], }, plugins: [ new HtmlWebPackPlugin({ favicon: "./src/assets/img/favicons/favicon.ico", template: "./src/html/index.html", filename: "./index.html", excludeChunks: ["server"], }), new webpack.HotModuleReplacementPlugin(), new webpack.NoEmitOnErrorsPlugin(), new Dotenv() ], }; -
webpack.prod
const path = require("path"); const webpack = require("webpack"); const HtmlWebPackPlugin = require("html-webpack-plugin"); const MiniCssExtractPlugin = require("mini-css-extract-plugin"); const paths = require("./config/paths"); const isDevelopment = false; const Dotenv = require('dotenv-webpack'); require('dotenv').config() module.exports = { entry: { main: [ "webpack-hot-middleware/client?path=/__webpack_hmr&timeout=20000", paths.appIndexJs, ], }, output: { path: path.join(__dirname, "dist"), publicPath: "/", filename: "[name].js", }, devServer: { historyApiFallback: true, }, mode: "production", target: "web", devtool: "#source-map", module: { rules: [ { enforce: "pre", test: /\.(js|jsx)$/, exclude: /node_modules/, loader: "eslint-loader", options: { emitWarning: true, failOnError: false, failOnWarning: false, }, }, { test: /\.(js|jsx)$/, exclude: /node_modules/, include: path.resolve(paths.appSrc), loader: "babel-loader", }, { // Loads the javacript into html template provided. // Entry point is set below in HtmlWebPackPlugin in Plugins test: /\.html$/, include: path.resolve(paths.appSrc), use: [ { loader: "html-loader", }, ], }, { test: /\.css$/, include: path.resolve(paths.appSrc), use: ["style-loader", "css-loader"], }, { test: /\.css$/, include: /node_modules/, use: ["style-loader", "css-loader"], }, { test: /\.scss$/, use: ["style-loader", "css-loader", "sass-loader"], }, /*{ test: /\.scss$/, use: [ { loader: 'css-loader', options: { url: false, sourceMap: true } }, { loader: 'sass-loader', options: { sourceMap: true } }, ] },*/ /*{ test: /\.css$/, include: path.resolve(paths.appSrc), use: ["style-loader", "css-loader"], }, { test: /\.sass$/, use: [ MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { url: false, sourceMap: true } }, { loader: 'sass-loader', options: { sourceMap: true } }, ] }, { test: /\.css$/, include: /node_modules/, loader: [ isDevelopment ? "style-loader" : MiniCssExtractPlugin.loader, { loader: "css-loader", options: { modules: true, sourceMap: isDevelopment, }, }, ], }, { test: /\.(scss)$/, use: [ MiniCssExtractPlugin.loader, { loader: 'css-loader', options: { url: false, sourceMap: true } }, { loader: 'sass-loader', options: { sourceMap: true } }, ] },*/ { test: /\.(png|svg|jpg|gif|eot|woff|woff2|ttf)$/, //include: path.resolve(paths.appSrc), use: ["file-loader"], }, ], }, resolve: { extensions: [".js", ".jsx"], }, plugins: [ new webpack.DefinePlugin({ 'process.env': { 'NODE_ENV': JSON.stringify('production') } }), new HtmlWebPackPlugin({ favicon: "./src/assets/img/favicons/favicon.ico", template: "./src/html/index.html", filename: "./index.html", excludeChunks: ["server"], }), new webpack.NoEmitOnErrorsPlugin(), new Dotenv() ], }; -
webpack.server
const path = require("path"); const webpack = require("webpack"); const nodeExternals = require("webpack-node-externals"); const HtmlWebPackPlugin = require("html-webpack-plugin"); module.exports = (env, argv) => { const SERVER_PATH = argv.mode === "production" ? "./src/server/server-prod.js" : "./src/server/server-dev.js"; return { entry: { server: SERVER_PATH, }, output: { path: path.join(__dirname, "dist"), publicPath: "/", filename: "[name].js", }, mode: argv.mode, target: "node", node: { // Need this when working with express, otherwise the build fails __dirname: false, // if you don't put this is, __dirname __filename: false, // and __filename return blank or / }, externals: [nodeExternals()], // Need this to avoid error when working with Express devServer: { historyApiFallback: true, }, module: { rules: [ { // Transpiles ES6-8 into ES5 test: /\.js$/, exclude: /node_modules/, use: { loader: "babel-loader", }, } ], }, }; };
【问题讨论】:
标签: node.js express webpack dotenv