【问题标题】:Can't read a file inside functions folder无法读取函数文件夹中的文件
【发布时间】:2018-05-01 01:49:26
【问题描述】:

我有一个在 Firebase 上运行的 expressjs 应用程序,我正在尝试从我的 firebase-express/functions/index.js 读取位于 functions/api/swagger/swagger.yaml 的文件。

const swaggerDocument = YAML.load('./api/swagger/swagger.yaml');

当我使用 firebase serve 在本地运行时,我收到以下错误。

Error: ENOENT: no such file or directory, open 'C:\Users\Tulio\Desktop\firebase-express\api\swagger\swagger.yaml'
    at Error (native)
    at Object.fs.openSync (fs.js:642:18)
    at Object.fs.readFileSync (fs.js:510:33)
    at Function.Utils.getStringFromFile (C:\Users\Tulio\Desktop\firebase-express\functions\node_modules\yamljs\lib\Utils.js:284:19)
    at Function.Yaml.parseFile (C:\Users\Tulio\Desktop\firebase-express\functions\node_modules\yamljs\lib\Yaml.js:46:21)
    at Function.Yaml.load (C:\Users\Tulio\Desktop\firebase-express\functions\node_modules\yamljs\lib\Yaml.js:78:17)
    at Object.<anonymous> (C:\Users\Tulio\Desktop\firebase-express\functions\index.js:11:30)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)

它正在寻找'C:\Users\Tulio\Desktop\firebase-express\api\swagger\swagger.yaml' 的文件,所以解决方案必须是更改我的参数以包含文件夹'functions'对吗?

const swaggerDocument = YAML.load('./functions/api/swagger/swagger.yaml');

现在我收到另一个错误,路径显示为:

no such file or directory, open 'C:\Users\Tulio\Desktop\firebase-express\functions\functions\api\swagger\swagger.yaml'

注意到'functions\functions'了吗?出于某种原因,当我将“functions/”添加到路径时,它会附加另一个“functions/”,从而使路径无效。

如何获取functions 文件夹中文件的正确路径?

【问题讨论】:

    标签: node.js firebase express


    【解决方案1】:

    使用路径

    var path = require('path');
    var swagger_path =  path.resolve(__dirname,'./api/swagger/swagger.yaml');
    console.log(swagger_path);
    

    这将在任何环境中获取正确的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-21
      • 2018-11-12
      • 2016-10-09
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      相关资源
      最近更新 更多