首页 > 解决方案 > 如何在无服务器中获取项目内部的文件路径

问题描述

我有一个节点 lambda 函数,可以读取其中的打包文件的内容。目录结构如下。

example1-folder // folder inside project
-> fns // in this folder all the functions/Lambda are written
   -> index.html // the file which is trying to read the file

example2-folder
-> fns // in this folder all the functions/Lambda are written
   -> index.html

data.txt // in the main project directory the file is present

然后在 example1-folder => index.js 文件中,我编写了以下代码来获取文件的路径。

let path = require("path");

exports.handler = (event, context, callback) => {
        fs.readFile(__dirname +'/data.txt', function (err, data) {
            if (err) throw err;
        });
};

我不断收到以下错误:

[Error: ENOENT: no such file or directory, open '/var/task/data.txt']

请帮我解决问题。如果需要更多信息,请告诉我。

标签: node.jsamazon-web-servicesaws-lambdaserverless

解决方案


推荐阅读