Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

xlsx is not a function #87

Open
offeex opened this issue Oct 20, 2023 · 3 comments
Open

xlsx is not a function #87

offeex opened this issue Oct 20, 2023 · 3 comments
Assignees

Comments

@offeex
Copy link

offeex commented Oct 20, 2023

import {IJsonSheet, xlsx} from "json-as-xlsx"

const data: IJsonSheet[] = [{
  sheet: 'BazaDizainerov-Kyiv-15-50',
  columns: [
    { label: 'name', value: 'name' },
    { label: 'email', value: 'email' },
    { label: 'phone', value: 'phone' },
    { label: 'city', value: 'city' },
    { label: 'company_type', value: 'company_type' },
    { label: 'experience', value: 'experience' },
    { label: 'portfolio', value: 'portfolio' },
    { label: 'price', value: 'price' },
    { label: 'opys_robit', value: 'opys_robit' }
  ],
  content: products
}]

fs.writeFileSync('data.xlsx', xlsx(data))

Here's what i get:

TypeError: xlsx is not a function
    at file:///home/offeex/Code/Standalone/designer-parser/index.ts:54:31
    at ModuleJob.run (node:internal/modules/esm/module_job:217:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:316:24)
    at async loadESM (node:internal/process/esm_loader:34:7)
    at async handleMainPromise (node:internal/modules/run_main:66:12)
@offeex offeex changed the title cannot use xlsx function xlsx is not a function Oct 20, 2023
@LuisEnMarroquin LuisEnMarroquin self-assigned this Oct 22, 2023
@flamngoapp
Copy link

flamngoapp commented Oct 22, 2023

Hey, I am getting the same error. When I import all from 'json-as-xlsx' I can actually see that the exported xlsx property is undefined.

@LuisEnMarroquin
Copy link
Owner

LuisEnMarroquin commented Oct 23, 2023

Hi, not sure why this is happenning, last build was a day ago and this issue was reported 3 days ago, if anyone has an idea/PR I can happily merge

image

https://www.npmjs.com/package/json-as-xlsx?activeTab=versions

I also tried with a demo project

  • package.json
{
  "name": "json-text",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "npx ts-node index.ts"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "json-as-xlsx": "^2.5.5"
  },
  "devDependencies": {
    "@types/node": "^20.8.7"
  }
}
  • tsconfig.json
{
  "compilerOptions": {
    "types": ["node"],
    "moduleResolution": "node",
    "target": "es2018"
  }
}
  • index.ts
import xlsx, { IJsonSheet } from "json-as-xlsx";
import { writeFileSync } from "fs";

const products = [
  {
    name: "John Doe",
    email: "[email protected]",
    phone: "+1234567890",
    city: "New York",
    company_type: "Software Development",
    experience: "5 years",
    portfolio: "www.johndoeportfolio.com",
    price: "$5000",
    opys_robit: "Web development, mobile applications, UI/UX design",
  },
  {
    name: "Jane Smith",
    email: "[email protected]",
    phone: "+0987654321",
    city: "Los Angeles",
    company_type: "Graphic Design",
    experience: "3 years",
    portfolio: "www.janesmithdesigns.com",
    price: "$4000",
    opys_robit: "Logo design, branding, print design",
  },
];

const data: IJsonSheet[] = [
  {
    sheet: "BazaDizainerov-Kyiv-15-50",
    columns: [
      { label: "name", value: "name" },
      { label: "email", value: "email" },
      { label: "phone", value: "phone" },
      { label: "city", value: "city" },
      { label: "company_type", value: "company_type" },
      { label: "experience", value: "experience" },
      { label: "portfolio", value: "portfolio" },
      { label: "price", value: "price" },
      { label: "opys_robit", value: "opys_robit" },
    ],
    content: products,
  },
];

writeFileSync("data.xlsx", xlsx(data));

this is also the error than I am getting

json-text ☭ nr start

> [email protected] start
> npx ts-node index.ts

/Users/XXX/Projects/json-text/index.ts:47
writeFileSync("data.xlsx", xlsx(data));
                               ^
TypeError: (0 , json_as_xlsx_1.default) is not a function
    at Object.<anonymous> (/Users/XXX/Projects/json-text/index.ts:47:32)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Module.m._compile (/Users/XXX/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1618:23)
    at Module._extensions..js (node:internal/modules/cjs/loader:1157:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/XXX/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at phase4 (/Users/XXX/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/bin.ts:649:14)
    at bootstrap (/Users/XXX/.npm/_npx/1bf7c3c15bf47d04/node_modules/ts-node/src/bin.ts:95:10)
json-text ☭ 

@dev-talha-anwar
Copy link

i was getting same error solved it by using require instead of import like this

// eslint-disable-next-line @typescript-eslint/no-var-requires
const xlsx = require('json-as-xlsx');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants