在Firebase函数中:;找不到模块'xlsx'&"



我正在尝试读取firebase函数中的excel文件。我已选择使用库"xlsx"(链接(。我用npm install xlsx安装了这个库,它运行得很好,还将这个库作为"xlsx": "^0.15.5"添加到了我的package.json依赖项中。我需要const XLSX = require('xlsx');的firebase函数中的模块,但随后我收到以下错误:

Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'xlsx'
at Function.Module._resolveFilename (module.js:548:15)
at Function.Module._load (module.js:475:25)
at Module.require (module.js:597:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (/srv/index.js:21:14)
at Module._compile (module.js:653:30)
at Object.Module._extensions..js (module.js:664:10)
at Module.load (module.js:566:32)
at tryModuleLoad (module.js:506:12)
at Function.Module._load (module.js:498:3)

有什么帮助吗?


为清晰起见进行编辑。谢谢你的提问。这是package.json。您会注意到我前面在依赖项列表中提到的行。

{
"name": "------",
"version": "----",
"private": true,
"dependencies": {
"@google-cloud/storage": "^4.5.0",
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.4.1",
"@testing-library/user-event": "^7.2.1",
"firebase": "^7.9.1",
"react": "^16.12.0",
"react-dom": "^16.12.0",
"react-firebaseui": "^4.1.0",
"react-scripts": "3.4.0",
"xlsx": "^0.15.5"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

下面是失败的代码:

const path = require('path');
const os = require('os');
const xlsx = require('xlsx');
exports.refreshRightFile = functions.storage.object().onFinalize(async (object) => {
const fileBucket = object.bucket; // The Storage bucket that contains the file.
const filePath = object.name; // File path in the bucket.
const contentType = object.contentType; // File content type.
const metageneration = object.metageneration; // Number of times metadata has been generated. New objects have a value of 1.
// Get the file name.
const fileName = path.basename(filePath);
// Exit if this is triggered on any file except CourseMaster.xlsx
if (fileName !== 'right_file.xlsx') {
return console.log('This is not the right file.');
} else {
console.log('The right file has changed.');
}
// Download file from bucket.
const bucket = admin.storage().bucket(fileBucket);
const tempFilePath = path.join(os.tmpdir(), fileName);
const metadata = {
contentType: contentType,
};
await bucket.file(filePath).download({destination: tempFilePath});
console.log('The right file has downloaded locally to', tempFilePath);
courseMasterWorkbook = xlsx.readfile(tempFilePath);
console.log(xlsx.utils.sheet_to_json(courseMasterWorkbook.Sheets['Sheet1']))
});

在@Renaud Tarnec评论的引导下,我注意到函数文件夹中有一个单独的package.json。这缺少xlsx依赖项。我添加了它,一切都很好!谢谢

相关内容

  • 没有找到相关文章

最新更新