如何在tsconfig.json中包含.env文件以供编译器使用。

huangapple go评论58阅读模式
英文:

How to include .env file in tsconfig.json for transpiler

问题

我有一个类似这样的typescript项目结构:

```bash
根目录/
  应用/
    index.ts
  配置/
    .env.dev

在index.ts中,我想使用dotenv.config()来配置指向.env.dev文件的路径:

import * as dotenv from 'dotenv';
import path from 'path';

// 设置 __dirname,等等
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// 手动定义 NODE_ENV 为 NODE_ENV=dev
const dotenvPath = path.join(__dirname, '../', `config/.env.${process.env.NODE_ENV}`);
dotenv.config({
  path: dotenvPath,
});

console.log(`dotenvPath: ${dotenvPath}`)
console.log(`${process.env.SOMEVAR_INSIDE_ENVDEV_FILE}`) // <-- undefined

然而,当我尝试使用 process.env.SOMEVAR_INSIDE_ENVDEV_FILE 时,它是未定义的。

console.log(`dotenvPath: ${dotenvPath}`) 输出:

dotenvPath: C:\Users\myuser\path\to\project\.build\config\.env.dev

然而,.build\config\ 似乎不存在。这是我的 tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "esModuleInterop": true,
    "moduleResolution": "node",
    "forceConsistentCasingInFileNames": true,
    "outDir": "./build",
    "rootDir": "./app"
  },
  "exclude": ["node_modules"]
}

"exclude" 标签之后添加以下标签:

"include": [
    "./config/*",
 ],

会给我带来一个我在其中找不到太多信息的奇怪错误:Error: [{"messageText":"在配置文件 'tsconfig.json' 中找不到输入。指定的 'include' 路径为 '[\"./config/*\"]',而 'exclude' 路径为 '[\"node_modules\"]'。","category":1,"code":18003}]。我找到的唯一资源是这里,但在我这种情况下并没有太多意义,因为我认为我所做的一切都没有错。

我如何强制typescript将我的 ./config/.env.dev 文件包含在转译过程中,以便我可以在运行时访问它?


<details>
<summary>英文:</summary>

I have a typescript project structure like this: 

```bash
root/
  app/
    index.ts
  config/
    .env.dev

Inside index.ts, I want to call dotenv.config() with the path pointing to the .env.dev file to configure it:

import * as dotenv from &#39;dotenv&#39;;
import path from &#39;path&#39;;

// set __dirname, yada yada yada
import { fileURLToPath } from &#39;url&#39;;
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

// NODE_ENV is defined manually to NODE_ENV=dev
const dotenvPath = path.join(__dirname, &#39;../&#39;, `config/.env.${process.env.NODE_ENV}`);
dotenv.config({
  path: dotenvPath,
});

console.log(`dotenvPath: ${dotenvPath}`)
console.log(`${process.env.SOMEVAR_INSIDE_ENVDEV_FILE}`) // &lt;-- undefined

However, when I try to use process.env.SOMEVAR_INSIDE_ENVDEV_FILE, it is undefined.

The line console.log(`dotenvPath: ${dotenvPath}`) outputs:

dotenvPath: C:\Users\myuser\path\to\project\.build\config\.env.dev

However, .build\config\ seems to not exist. This is my tsconfig.json:

{
  &quot;compilerOptions&quot;: {
    &quot;target&quot;: &quot;esnext&quot;,
    &quot;module&quot;: &quot;esnext&quot;,
    &quot;strict&quot;: true,
    &quot;esModuleInterop&quot;: true,
    &quot;moduleResolution&quot;: &quot;node&quot;,
    &quot;forceConsistentCasingInFileNames&quot;: true,
    &quot;outDir&quot;: &quot;./build&quot;,
    &quot;rootDir&quot;: &quot;./app&quot;
  },
  &quot;exclude&quot;: [&quot;node_modules&quot;]
}

Adding the following tag after &quot;exclude&quot; tag:

&quot;include&quot;: [
    &quot;./config/*&quot;,
 ],

gives me a weird error that I could not find much about: Error: [{&quot;messageText&quot;:&quot;No inputs were found in config file &#39;tsconfig.json&#39;. Specified &#39;include&#39; paths were &#39;[\&quot;./config/*\&quot;]&#39; and &#39;exclude&#39; paths were &#39;[\&quot;node_modules\&quot;]&#39;.&quot;,&quot;category&quot;:1,&quot;code&quot;:18003}]. Only resource I found was this, but it doesn't make much sense in my case, because I find nothing wrong with what I did.

How can I force typescript to include my ./config/.env.dev file into the transpilation process, so that I can access it during runtime?

答案1

得分: 1

Typescript 只会转译 Typescript 文件和可能的 Javascript 文件。如果你只想编写一个脚本,以在文件发生更改时复制它,那么采用像 Make 这样的构建工具似乎是一个不错的选择。

英文:

Typescript only transpiles Typescript files and potentially Javascript files. If you just want to write a script that copies a file if it has changed, this seems like a good problem for adopting a build tool like Make.

huangapple
  • 本文由 发表于 2023年7月20日 08:22:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76725944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定