简体   繁体   English

ts-node 编译器没有读取环境变量

[英]ts-node compiler is not reading environment variables

I'm not sure if any of you ever came across the same issue but I've got a problem with ts-node and environment variables and your help would be much appreciated!我不确定你们中是否有人遇到过同样的问题,但我遇到了ts-node和环境变量的问题,非常感谢您的帮助!

ts-node is not reading the environment variables ts-node 没有读取环境变量

What I'm trying to do here is basically just to run index.ts with ts-node .我在这里要做的基本上只是用ts-node运行index.ts And this index.ts simply prints out one of the environment variables.而这个index.ts只是打印出一个环境变量。

index.ts索引.ts

import "dotenv/config";

const printEnv = (value: string) => {
  console.log(value);
};

printEnv(process.env.PASSWORD);

To make the environment variables available from index.ts , I created env.d.ts in which I defined the type of the variable.为了让env.d.ts index.ts在其中定义了变量的类型。

env.d.ts环境.d.ts

declare global {
  namespace NodeJS {
    interface ProcessEnv {
      PASSWORD: string;
    }
  }
}
export {}

I set up my tsconfig.json , package.json and .env files like below.我设置了我的tsconfig.jsonpackage.json.env文件,如下所示。

tsconfig.json tsconfig.json

{
  "name": "ts-env",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/index.ts"
  },
  "devDependencies": {
    "@types/node": "^18.6.1",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "dotenv": "^16.0.1"
  }
}

package.json package.json

{
  "name": "ts-env",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "scripts": {
    "start": "nodemon src/index.ts"
  },
  "devDependencies": {
    "@types/node": "^18.6.1",
    "nodemon": "^2.0.19",
    "ts-node": "^10.9.1",
    "typescript": "^4.7.4"
  },
  "dependencies": {
    "dotenv": "^16.0.1"
  }
}

.env .env

PASSWORD=test0123

With this setup, VSCode is now able to show me the possible options whenever I type process.env.通过此设置,VSCode 现在能够在我键入process.env. 在此处输入图像描述

But it fails to compile whenever I try to run this application with ts-node , saying that process.env.PASSWORD could be undefined.但是每当我尝试使用ts-node运行此应用程序时,它都无法编译,说process.env.PASSWORD可能未定义。

Here is the error log from ts-node .这是来自ts-node的错误日志。

TSError: ⨯ Unable to compile TypeScript:
src/index.ts:9:10 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

9 printEnv(process.env.PASSWORD);

I did fair amount of digging but unable to find a right solution for this issue.我做了相当多的挖掘,但无法找到解决这个问题的正确方法。 It would be much appreciated if you could point me in the right direction.如果您能指出我正确的方向,将不胜感激。

The type for an environment variable is string | undefined环境变量的类型是string | undefined string | undefined , which makes sense because it can miss, either the entire .env file, or just that the variable is not in it. string | undefined ,这是有道理的,因为它可能会丢失整个.env文件,或者只是该变量不在其中。

The fix here is either to change your function definition to:此处的解决方法是将您的 function 定义更改为:

const printEnv = (value: string | undefined) => {
  console.log(value);
};

printEnv(process.env.PASSWORD);

Or tell TypeScript that the variable PASSWORD exist for sure with the !或者告诉 TypeScript 变量PASSWORD肯定存在! mark, like so:标记,像这样:

const printEnv = (value: string) => {
  console.log(value);
};

printEnv(process.env.PASSWORD!);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM