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

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

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

问题

  1. 我有一个类似这样的typescript项目结构:
  2. ```bash
  3. 根目录/
  4. 应用/
  5. index.ts
  6. 配置/
  7. .env.dev

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

  1. import * as dotenv from 'dotenv';
  2. import path from 'path';
  3. // 设置 __dirname,等等
  4. import { fileURLToPath } from 'url';
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = path.dirname(__filename);
  7. // 手动定义 NODE_ENV 为 NODE_ENV=dev
  8. const dotenvPath = path.join(__dirname, '../', `config/.env.${process.env.NODE_ENV}`);
  9. dotenv.config({
  10. path: dotenvPath,
  11. });
  12. console.log(`dotenvPath: ${dotenvPath}`)
  13. console.log(`${process.env.SOMEVAR_INSIDE_ENVDEV_FILE}`) // <-- undefined

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

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

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

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

  1. {
  2. "compilerOptions": {
  3. "target": "esnext",
  4. "module": "esnext",
  5. "strict": true,
  6. "esModuleInterop": true,
  7. "moduleResolution": "node",
  8. "forceConsistentCasingInFileNames": true,
  9. "outDir": "./build",
  10. "rootDir": "./app"
  11. },
  12. "exclude": ["node_modules"]
  13. }

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

  1. "include": [
  2. "./config/*",
  3. ],

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

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

  1. <details>
  2. <summary>英文:</summary>
  3. I have a typescript project structure like this:
  4. ```bash
  5. root/
  6. app/
  7. index.ts
  8. config/
  9. .env.dev

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

  1. import * as dotenv from &#39;dotenv&#39;;
  2. import path from &#39;path&#39;;
  3. // set __dirname, yada yada yada
  4. import { fileURLToPath } from &#39;url&#39;;
  5. const __filename = fileURLToPath(import.meta.url);
  6. const __dirname = path.dirname(__filename);
  7. // NODE_ENV is defined manually to NODE_ENV=dev
  8. const dotenvPath = path.join(__dirname, &#39;../&#39;, `config/.env.${process.env.NODE_ENV}`);
  9. dotenv.config({
  10. path: dotenvPath,
  11. });
  12. console.log(`dotenvPath: ${dotenvPath}`)
  13. 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:

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

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

  1. {
  2. &quot;compilerOptions&quot;: {
  3. &quot;target&quot;: &quot;esnext&quot;,
  4. &quot;module&quot;: &quot;esnext&quot;,
  5. &quot;strict&quot;: true,
  6. &quot;esModuleInterop&quot;: true,
  7. &quot;moduleResolution&quot;: &quot;node&quot;,
  8. &quot;forceConsistentCasingInFileNames&quot;: true,
  9. &quot;outDir&quot;: &quot;./build&quot;,
  10. &quot;rootDir&quot;: &quot;./app&quot;
  11. },
  12. &quot;exclude&quot;: [&quot;node_modules&quot;]
  13. }

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

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

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:

确定