【问题标题】:node js webpack Undefined envioronment variablenode js webpack 未定义的环境变量
【发布时间】:2020-02-24 00:01:58
【问题描述】:

嘿嘿,

我正在尝试使用 .env 文件来存储我正在使用的 API 的 API id 和密钥,但它不起作用。我的 .env 文件位于我的工作目录的根目录以及 .gitignore 文件所在的存储库的根目录中。它看起来像这样:

API_ID=XXXXXX
API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXX

我安装了 dotenv,并在我的服务器目录中的 index.js 文件中这样称呼它:

const dotenv = require('dotenv');
dotenv.config();

然后在我的 apiCall.js 函数中,我尝试从 .env 文件中调用变量,如下所示:

const AYLIENTextAPI = require('aylien_textapi');

let textapi = new AYLIENTextAPI({
    application_id: process.env.API_ID,
    application_key: process.env.API_KEY
});

但 ID 和密钥显示为未定义。我究竟做错了什么?非常感谢任何帮助。

谢谢, 迈克

更新:

这是我的 webpack.dev.js 文件:

const path = require('path')
const webpack = require('webpack')
const HtmlWebPackPlugin = require("html-webpack-plugin")
const { CleanWebpackPlugin } = require('clean-webpack-plugin')

module.exports = {
    entry: './src/client/index.js',
    output : {
        libraryTarget: 'var',
        library: 'Client'
    },
    mode: 'development',
    devtool: 'source-map',
    stats: 'verbose',
    module: {
        rules: [
            {
                test: '/\.js$/',
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.scss$/,
                use: [ 'style-loader', 'css-loader', 'sass-loader' ]
            }
        ]
    },
    plugins: [
        new HtmlWebPackPlugin({
            template: "./src/client/views/index.html",
            filename: "./index.html",
        }),
        new CleanWebpackPlugin({
            // Simulate the removal of files
            dry: true,
            // Write Logs to Console
            verbose: true,
            // Automatically remove all unused webpack assets on rebuild
            cleanStaleWebpackAssets: true,
            protectWebpackAssets: false
        })
    ]
}

这是我的 package.json 文件:

{
  "name": "example-project",
  "version": "1.0.0",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node src/server/index.js",
    "build-prod": "webpack --config webpack.prod.js",
    "build-dev": "webpack  --config webpack.dev.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "description": "",
  "dependencies": {
    "aylien_textapi": "^0.7.0",
    "dotenv": "^8.2.0",
    "express": "^4.17.1",
    "webpack": "^4.41.6",
    "webpack-cli": "^3.3.11"
  },
  "devDependencies": {
    "@babel/core": "^7.8.4",
    "@babel/preset-env": "^7.8.4",
    "babel-loader": "^8.0.6",
    "clean-webpack-plugin": "^3.0.0",
    "css-loader": "^3.4.2",
    "html-webpack-plugin": "^3.2.0",
    "mini-css-extract-plugin": "^0.9.0",
    "node-sass": "^4.13.1",
    "optimize-css-assets-webpack-plugin": "^5.0.3",
    "sass-loader": "^8.0.2",
    "style-loader": "^1.1.3",
    "terser-webpack-plugin": "^2.3.5",
    "webpack-dev-server": "^3.10.3",
    "workbox-webpack-plugin": "^5.0.0"
  }
}

【问题讨论】:

  • 确保您的 .env 文件位于项目根文件夹中。如果您的 .env 文件在根文件夹中并且您仍然未定义,请尝试使用 require('dotenv').config({path: __dirname + '/.env'})
  • 那行不通。还有其他想法吗?
  • 你能用你的webpack.config.jspackage.json更新问题吗?
  • 好的,我发布了它们。这有帮助吗?

标签: node.js express webpack


【解决方案1】:

想通了。我只能访问服务器端的环境变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-09
    • 1970-01-01
    • 2020-03-27
    • 1970-01-01
    • 1970-01-01
    • 2020-08-09
    • 2021-08-15
    • 1970-01-01
    相关资源
    最近更新 更多