How to resolve error: Missing the environment variable 'TWITTER_CONSUMER_KEY'

How to resolve error: Missing the environment variable 'TWITTER_CONSUMER_KEY'

我是编程初学者,所以最不希望我会遇到任何错误。这一天,将一些 Node.js 应用程序编码到 post Twitter Feeds to Discord 但未能这样做。

虽然我已经为 .env 文件提供了必要的数据,但我还是不明白为什么会看到这个错误。已经 运行 必要的依赖项喜欢,npm install gmnpm i dotenv。代码如下。

'use strict';

const fs = require('fs');
const commandExistsSync = require('command-exists').sync;
const logger = require('./bin/logger');
const { version } = require('./package.json');

logger.info(`Starting the twitter-to-discord application v${version}`);

// Extract all the env variables we will be using
const {
  TWITTER_CONSUMER_KEYS,
  TWITTER_CONSUMER_SECRET,
  TWITTER_ACCESS_TOKEN_KEY,
  TWITTER_ACCESS_TOKEN_SECRET,
  MONGO_URI,
  DISCORD_BOT_TOKEN,
  DISCORD_CMD_PREFIX,
  DISCORD_BOT_OWNER_ID,
  TEMP,
} = process.env;

// Exit if the env variable was not set or passed. None can be empty
function envTest(value, name) {
  logger.debug(`${name}=${value}`);
  if (!value) {
    logger.error(`Missing the environment variable '${name}'`);
    process.exit(1);
  }
}

envTest(TWITTER_CONSUMER_KEYS, 'TWITTER_CONSUMER_KEYS');
envTest(TWITTER_CONSUMER_SECRET, 'TWITTER_CONSUMER_SECRET');
envTest(TWITTER_ACCESS_TOKEN_KEY, 'TWITTER_ACCESS_TOKEN_KEY');
envTest(TWITTER_ACCESS_TOKEN_SECRET, 'TWITTER_ACCESS_TOKEN_SECRET');
envTest(MONGO_URI, 'MONGO_URI');
envTest(DISCORD_BOT_TOKEN, 'DISCORD_BOT_TOKEN');
envTest(DISCORD_CMD_PREFIX, 'DISCORD_CMD_PREFIX');
envTest(DISCORD_BOT_OWNER_ID, 'DISCORD_BOT_OWNER_ID');
envTest(TEMP, 'TEMP');

// Ensure we can access the temp directory
try {
  fs.accessSync(process.env.TEMP, fs.constants.F_OK);
} catch (err) {
  logger.error(`Unable to access the temp directory: ${process.env.TEMP}`);
  logger.debug(err);
  process.exit(1);
}

// Ensure all the commands we need to function exist via PATH
if (!commandExistsSync('ffmpeg')) {
  logger.error('\'ffmpeg\' is not available on the command line');
  process.exit(1);
}
if (!commandExistsSync('gm')) {
  logger.error('\'gm\' is not available on the command line');
  process.exit(1);
}

// Passed all the startup tests
// Continue to load application
require('./bin');

尽管您已经安装了 dotenv,但您并没有 运行。你应该在你的程序开始时有 require('dotenv').config()