2***@qq.com
2***@qq.com
  • 发布:2023-01-14 23:03
  • 更新:2023-01-15 11:55
  • 阅读:300

云函数本地运行一切正常,调用云端云函数则报错,报错显示依赖报错

分类:uniCloud

新建了一个wordExport云函数,在云函数目录下npm安装officegen依赖

npm install officegen --save

云函数index.js中书写代码如下:


'use strict';  

const officegen = require('officegen');  

const fs = require('fs')  

const path = require('path')  

exports.main = async (event, context) => {  

    // 实例化docx  
    const docx = officegen('docx')  

    docx.on('finalize', function(written) {  
        console.log('临时word创建完成')  
    })  

    docx.on('error', function(err) {  
        console.log(err)  
        return err  
    })  

    let DOCXName = new Date().getTime().toString() + Math.floor(Math.random() * (999 - 111)) + '.docx'  

    // Create a new paragraph: 创建新段落  
    let pObj = docx.createP({  
        align: 'center'  
    })  

    // 段落新增文字行  
    pObj.addText('商品列表', {  
        bold: true,  
        font_face: 'Arial',  
        font_size: 18  
    })  

    // 定义表格样式  
    let tableStyle = {  
        tableColWidth: 3600,  
        tableSize: 24,  
        tableColor: "000000",  
        tableAlign: "center",  
        tableVAlign: "center",  
        tableFontFamily: "黑体",  
        borders: true  
    }  

    // 定义表格数据结构  
    let table = [  
        [{  
            val: "No",  
            opts: {  
                align: "center",  
                vAlign: "center",  
                b: true,  
                sz: '24',  
                fontFamily: "黑体"  
            }  
        }, {  
            val: "Title1",  
            opts: {  
                align: "center",  
                vAlign: "center",  
                b: true,  
                sz: '24',  
                fontFamily: "黑体"  
            }  
        }, {  
            val: "Title2",  
            opts: {  
                align: "center",  
                vAlign: "center",  
                b: true,  
                sz: '24',  
                fontFamily: "黑体"  
            }  
        }],  
        [1, '第一行', ''],  
        [2, '第二行', ''],  
        [3, '第三行', ''],  
        [4, '第四行', ''],  
        [5, '第五行', ''],  
        [6, '第六行', ''],  
        [7, '第七行', 'END'],  
    ]  

    // docx创建表格  
    docx.createTable(table, tableStyle)  

    // 增加分页符  
    docx.putPageBreak()  

    let out = fs.createWriteStream('/tmp/example.docx')  

    out.on('error', function(err) {  
        console.log(err);  
    });  

    let result = docx.generate(out);  

    // 把生成好的word文件保存到云存储里  
    let file_res = await uniCloud.uploadFile({  
        cloudPath:DOCXName,  
        fileContent:fs.createReadStream('/tmp/example.docx')  
    })  

    // 根据fileID获取该文件下载链接  
    let fileurl = await uniCloud.getTempFileURL({  
        fileList: [file_res.fileID]  
    });  
    return fileurl  

};  

本地运行起来一点问题没有(当然本地测试时候,临时生成Word文件路径是绝对路径,上传部署的时候才改为了"/tmp/example.docx")

[云端运行:腾讯云:]运行结果:(如下)

"errorCode":-1,  
"errorMessage":"user code exception caught",  
"requestId":"08495132-7da4-471d-9e24-580434741d01",  
"stackTrace":"Unexpected token * "  
....

报错详细信息为

/var/user/node_modules/readdir-glob/index.js:62  
    async function* exploreWalkAsync(dir, path, followSymlinks, useStat, shouldSkip, strict) {  
                  ^  
    SyntaxError: Unexpected token *  
        at createScript (vm.js:80:10)  
        at Object.runInThisContext (vm.js:139:10)  
        at Module._compile (module.js:607:28)  
        at Object.Module._extensions..js (module.js:654:10)  
        at Module.load (module.js:556:32)  
        at tryModuleLoad (module.js:499:12)  
        at Function.Module._load (module.js:491:3)  
        at Module.require (module.js:587:17)  
        at require (internal/module.js:11:18)  
        at Object.<anonymous> (/var/user/node_modules/archiver/lib/core.js:9:12)  

然后我在本地找到了 node_modules/readdir-glob/index.js文件中 62行,修改了写法,重新上传部署,但云服务器上的云函数还是没有跟随本地修改的内容而改变

是因为上传部署的时候,不会上传node_modules文件夹吗?而是根据package重新npm install吗?

那也没法去直接修改云服务器上的云函数文件,这应该怎么解决这种报错呢?

2023-01-14 23:03 负责人:无 分享
已邀请:
DCloud_uniCloud_CRL

DCloud_uniCloud_CRL

上传部署会重新npm i。

你这个错误是因为nodejs的版本不支持 async generator 导致的,尝试更换下node12试下。

  • 2***@qq.com (作者)

    好的,非常感谢,多谢指教,我尝试更换node版本试一下的

    2023-01-15 15:17

  • DCloud_uniCloud_WYQ

    回复 2***@qq.com: 不是换nodejs版本,是把这个依赖版本调低些

    2023-01-16 11:08

  • 2***@qq.com (作者)

    回复 DCloud_uniCloud_WYQ: 使用了node12版本,不再报错,调低依赖版本也是可行的,感谢指导[/抱拳]

    2023-01-16 22:16

要回复问题请先登录注册