英文:
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 'dotenv';
import path from 'path';
// set __dirname, yada yada yada
import { fileURLToPath } from 'url';
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, '../', `config/.env.${process.env.NODE_ENV}`);
dotenv.config({
path: dotenvPath,
});
console.log(`dotenvPath: ${dotenvPath}`)
console.log(`${process.env.SOMEVAR_INSIDE_ENVDEV_FILE}`) // <-- 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
:
{
"compilerOptions": {
"target": "esnext",
"module": "esnext",
"strict": true,
"esModuleInterop": true,
"moduleResolution": "node",
"forceConsistentCasingInFileNames": true,
"outDir": "./build",
"rootDir": "./app"
},
"exclude": ["node_modules"]
}
Adding the following tag after "exclude"
tag:
"include": [
"./config/*",
],
gives me a weird error that I could not find much about: Error: [{"messageText":"No inputs were found in config file 'tsconfig.json'. Specified 'include' paths were '[\"./config/*\"]' and 'exclude' paths were '[\"node_modules\"]'.","category":1,"code":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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论