解析触发器时出错:找不到模块“csv-parse/sync”

发布于 2025-01-09 17:59:13 字数 1735 浏览 1 评论 0原文

我正在使用 Firebase 函数构建一个解析 CSV 文件的 API。

当我尝试使用 csv-parse/sync 而不是 csv-parse 时,部署到 Firebase Functions 失败并出现以下错误:

Error: Error parsing triggers: Cannot find module 'csv-parse/sync''
Require stack:
- /Users/xxx/Programming/xxx/Firebase Functions/xxx/functions/lib/index.js
- /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js

Try running "npm install" in your functions directory before deploying.

我已使用以下方式导入:

import { parse } from 'csv-parse/sync';

然后在如下代码中使用:

interface EventData {
    update: string;
    id: string;
    title: string;
    description: string;
    category: string;
    ages: string;
    place: string;
    placeCoordinate: string;
    startDate: string;
    startTime: string;
    length: string;
    url: string;
    arrName: string;
  }

let events: Array<EventData> = []
const headers = ["update", "id", "title", "description", "ages", "place", "placeCoordinate", "startDate", "startTime", "length", "url", "arrEpost", "arrName", "validated", "skugg"]
try {
    events = parse(text, {columns: headers, from: 6, quote: "\"", delimiter: ";", ltrim: true, rtrim: true})
}...

我已通过转到 /functions-folder 并运行

npm install --save csv-parse

在根文件夹中部署

进行安装,使用firebase deploy

这是一个框架、firebase 的问题还是我做错了什么?正常使用没有同步的“csv-parse”就可以了。在这两种情况下,它似乎在 Visual Studio Code 中导入得很好,但在使用“sync”部署时则不然。我尝试清理node_modules文件夹,重建package-lock.json文件,升级到最新版本的firebase工具,但都没有成功。

我在框架项目问题页面上添加了类似的问题: https://github.com/ adaltas/node-csv/issues/323

I am using Firebase functions to build an API that parses CSV files.

When I try to use csv-parse/sync instead of csv-parse, my deploy to Firebase Functions fail with the following error:

Error: Error parsing triggers: Cannot find module 'csv-parse/sync''
Require stack:
- /Users/xxx/Programming/xxx/Firebase Functions/xxx/functions/lib/index.js
- /usr/local/lib/node_modules/firebase-tools/lib/deploy/functions/runtimes/node/triggerParser.js

Try running "npm install" in your functions directory before deploying.

I have imported using:

import { parse } from 'csv-parse/sync';

Then use in code like this:

interface EventData {
    update: string;
    id: string;
    title: string;
    description: string;
    category: string;
    ages: string;
    place: string;
    placeCoordinate: string;
    startDate: string;
    startTime: string;
    length: string;
    url: string;
    arrName: string;
  }

let events: Array<EventData> = []
const headers = ["update", "id", "title", "description", "ages", "place", "placeCoordinate", "startDate", "startTime", "length", "url", "arrEpost", "arrName", "validated", "skugg"]
try {
    events = parse(text, {columns: headers, from: 6, quote: "\"", delimiter: ";", ltrim: true, rtrim: true})
}...

I have installed by going to /functions-folder and running

npm install --save csv-parse

Deploying in root folder with

firebase deploy

Is this an issue with the framework, with firebase or am I doing something wrong? Normal use of "csv-parse" without sync works just fine. In both cases it seems to import just fine in Visual Studio Code, but not when deploying with "sync". I have tried to clean the node_modules folder, rebuild the package-lock.json-file, upgraded to the latest version of firebase tools, all without success.

I have added a similar question on the framework project issues page: https://github.com/adaltas/node-csv/issues/323

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

小霸王臭丫头 2025-01-16 17:59:13

如果您使用的是 Typescript,您可以执行以下操作

import  parse  from 'csv-parse/sync';

import  {parse}  from 'csv-parse/lib/sync';

If you are using Typescript you can do

import  parse  from 'csv-parse/sync';

or

import  {parse}  from 'csv-parse/lib/sync';
暮光沉寂 2025-01-16 17:59:13

对我来说,两者中的任何一个都在 node.js 中工作(基于 Jeff Paredes答案)

import parse from 'csv-parse/lib/sync';
const parse = require('csv-parse/lib/sync');

似乎 node_modules 文件夹中的文件夹结构已更改。因此,sync.js 不再位于 csv-parse 中,而是位于 csv-parse/lib 中。

输入图片此处描述

For me either of the two worked in node.js (based on Jeff Paredes answer):

import parse from 'csv-parse/lib/sync';
const parse = require('csv-parse/lib/sync');

It seems like the folder structure within the node_modules folder was changed. So instead of sync.js being located in csv-parse it is now in csv-parse/lib.

enter image description here

不离久伴 2025-01-16 17:59:13

使用 [email protected] (并且在拥有尝试单独使用 csv-parser ,但没有成功),以下是对我有用的唯一组合:

import { parse } from "csv-parse/sync";

示例用法:

import * as fs from "fs";
import { parse } from 'csv-parse/sync';

const csvPath = "./data/users.csv";

type UserRow = {
  id: number;
  name: string;
};

const csvContent = fs.readFileSync(csvPath, "utf-8");
const rows: UserRow[] = parse(csvContent, {
  columns: true,
  skip_empty_lines: true,
});

console.log(rows);

我还必须将 "moduleResolution": "node" 添加到 tsconfig.jsoncompilerOptions 中。

Using [email protected] (and after having tried using csv-parser on its own unsuccesfully), the following is the only combination that worked for me:

import { parse } from "csv-parse/sync";

Example usage:

import * as fs from "fs";
import { parse } from 'csv-parse/sync';

const csvPath = "./data/users.csv";

type UserRow = {
  id: number;
  name: string;
};

const csvContent = fs.readFileSync(csvPath, "utf-8");
const rows: UserRow[] = parse(csvContent, {
  columns: true,
  skip_empty_lines: true,
});

console.log(rows);

I also had to add "moduleResolution": "node" to the compilerOptions of tsconfig.json.

世界如花海般美丽 2025-01-16 17:59:13

从今天开始,如果您安装,则只需运行 yarn add csvpnpm install csvnpm install csv 即可安装 node-csv,

然后在你的代码就

import { parse } from 'csv'

完成了

As of today, if you install you should just install node-csv by running yarn add csv or pnpm install csv or npm install csv

Then in your code just do

import { parse } from 'csv'

Then you are done

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文