Nestjs读取文件 - 没有这样的文件或目录

发布于 2025-02-11 21:40:51 字数 790 浏览 1 评论 0原文

我很难获取我的Nestjs应用程序以阅读我的证书秘密文件,甚至是一个简单的TXT文件。我遇到了错误:

错误[exceptionshandler] enoent:没有这样的文件或目录

都没有使用FS来读取文件:

import {FireblocksSDK, PeerType, TransactionArguments, TransactionOperation, TransactionStatus} from "fireblocks-sdk";
import fs = require('fs');
import path = require('path');

function fireblocks() {
    const text = fs.readFileSync(('./data.txt'), 'utf8');
    console.log(text);
    const apiSecret = fs.readFileSync(path.resolve(__dirname, "../../certs/fireblocks_secret.key"), "utf8");
    return new FireblocksSDK(apiSecret, process.env.FIREBLOCKS_ACCESS_TOKEN);
}

以下是我正在使用的文件结构:

错误[exceptionshandler] enoent:没有这样的文件或目录

。 net/cpp1v.png“ alt =”在此处输入图像描述>

I am having trouble getting my nestJS app to read my cert secret file and even a simple txt file. I am getting the error:

ERROR [ExceptionsHandler] ENOENT: no such file or directory

Below is all I am doing, using fs to read the file:

import {FireblocksSDK, PeerType, TransactionArguments, TransactionOperation, TransactionStatus} from "fireblocks-sdk";
import fs = require('fs');
import path = require('path');

function fireblocks() {
    const text = fs.readFileSync(('./data.txt'), 'utf8');
    console.log(text);
    const apiSecret = fs.readFileSync(path.resolve(__dirname, "../../certs/fireblocks_secret.key"), "utf8");
    return new FireblocksSDK(apiSecret, process.env.FIREBLOCKS_ACCESS_TOKEN);
}

Below is my file structure I am using:

ERROR [ExceptionsHandler] ENOENT: no such file or directory

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

请你别敷衍 2025-02-18 21:40:52

Michael Levi 推荐path.join(Process.cwd(),...,...)

因此,要在Nestjs中的CERT文件中阅读,我使用了以下行:

 const apiSecret = fs.readFileSync(join(process.cwd(), './src/services/fireblocks/fireblocks_secret.key')).toString();

Michael Levi recommended path.join(process.cwd(), ...) in a comment.

So, to read in the cert file in NestJS, I used the line below:

 const apiSecret = fs.readFileSync(join(process.cwd(), './src/services/fireblocks/fireblocks_secret.key')).toString();
北座城市 2025-02-18 21:40:52

迈克尔·李维(Michael Levi)的建议,它在我的项目中对我有用,并在导入JSON文件时以这种方式应用了它:

import * as fs from 'fs';
import * as path from 'path';
const filePath = path.join(process.cwd(), './src/api/send-message/conf/conf.json');
const configFIle = fs.readFileSync(filePath, 'utf-8').toString();
export const config = JSON.parse(configFIle);

Michael Levi's recommendation, it worked for me in my project and I applied it in this way as I import a json file:

import * as fs from 'fs';
import * as path from 'path';
const filePath = path.join(process.cwd(), './src/api/send-message/conf/conf.json');
const configFIle = fs.readFileSync(filePath, 'utf-8').toString();
export const config = JSON.parse(configFIle);
几度春秋 2025-02-18 21:40:52

这将寻找test.json位于实际main.ts的相对路径,构建输出main.ts可能与源文件main.ts
$ {project_root}/build/assets/test.json$ {project_root}/src/assets/test.json

// main.ts
const filePath = path.join(__dirname, 'assets/test.json');

这将寻找位置的文件,您运行命令,例如节点构建/main,然后test.json$ {project_root}/build/assets.test.test.json

// main.ts
const filePath = path.join(process.cwd(), 'assets/test.json');

this will look for test.json located relative path to the actual main.ts, build output main.ts might not be the same as source file main.ts.
${PROJECT_ROOT}/build/assets/test.json or ${PROJECT_ROOT}/src/assets/test.json

// main.ts
const filePath = path.join(__dirname, 'assets/test.json');

this will look for file located relative path to process where you run the command, for example node build/main, then test.json is in ${PROJECT_ROOT}/build/assets.test.json

// main.ts
const filePath = path.join(process.cwd(), 'assets/test.json');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文