Nestjs读取文件 - 没有这样的文件或目录
我很难获取我的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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Michael Levi 推荐
path.join(Process.cwd(),...,...)
在因此,要在Nestjs中的CERT文件中阅读,我使用了以下行:
Michael Levi recommended
path.join(process.cwd(), ...)
in a comment.So, to read in the cert file in NestJS, I used the line below:
迈克尔·李维(Michael Levi)的建议,它在我的项目中对我有用,并在导入JSON文件时以这种方式应用了它:
Michael Levi's recommendation, it worked for me in my project and I applied it in this way as I import a json file:
这将寻找
test.json
位于实际main.ts
的相对路径,构建输出main.ts
可能与源文件main.ts
。$ {project_root}/build/assets/test.json
或$ {project_root}/src/assets/test.json
这将寻找位置的文件,您运行命令,例如
节点构建/main
,然后test.json
在$ {project_root}/build/assets.test.test.json
this will look for
test.json
located relative path to the actualmain.ts
, build outputmain.ts
might not be the same as source filemain.ts
.${PROJECT_ROOT}/build/assets/test.json
or${PROJECT_ROOT}/src/assets/test.json
this will look for file located relative path to process where you run the command, for example
node build/main
, thentest.json
is in${PROJECT_ROOT}/build/assets.test.json