'Module not found: Can't resolve 'xlsx' in 'filelocation\node_modules\cptable-fixed-xlsx-style'

We have tried ServerSideRendering for our web application, so we acheived it using ( https://github.com/axeldelafosse/expo-next-monorepo-example ) custom mono-repo-application which will support both node and next js. While running my build command I am facing this can't resolve xlsx error in cptable-fixed-xlsx-style package.

I'm using nextjs latest version 12 and webpack 5 & cptable-fixed-xlsx-style 0.8.20

Erorr message in terminal:

../../node_modules/cptable-fixed-xlsx-style/ods.js
Module not found: Can't resolve 'xlsx' in 'filelocation\node_modules\cptable-fixed-xlsx-style'
Did you mean './xlsx'?
Requests that should resolve in the current directory need to start with './'.
Requests that start with a name are treated as module requests and resolve within module directories (node_modules).
If changing the source code is not an option there is also a resolve options called 'preferRelative' which tries to resolve these kind of requests in the current directory too.

Import trace for requested module:
../../node_modules/cptable-fixed-xlsx-style/xlsx.js
../app/src/components/Applications/Tabs/CreditTransactions.tsx
../app/LoanManagementApp.tsx
../app/App.tsx
./src/pages/index.tsx

Our next.config.js:

// @ts-check

/**
 * @type {import('next').NextConfig}
 **/
 
require('events').EventEmitter.defaultMaxListeners = Infinity; 

const { withExpo } = require('@expo/next-adapter')
const withPlugins = require('next-compose-plugins')
const withFonts = require('next-fonts')
const withBundleAnalyzer = require('@next/bundle-analyzer')({
  enabled: process.env.ANALYZE === 'true'
})
const withTM = require('next-transpile-modules')([
  'app',
  '@gorhom/bottom-sheet',
  '@gorhom/portal',
  'dripsy',
  '@dripsy/core',
  'recyclerlistview'
])

const nextConfig = {
  typescript: {
    ignoreBuildErrors: true,
  },
  compress: true,
  webpack: (config) => {
    config.resolve.fallback = { fs: false };

    return config;
  },
}

module.exports = withPlugins(
  [
    withTM,
    withFonts,
    withBundleAnalyzer,
    [withExpo, { projectRoot: __dirname + '/../..' }]
  ],
  nextConfig
)

ods.js

/* ods.js (C) 2014 SheetJS -- http://sheetjs.com */
/* vim: set ts=2: */
/*jshint -W041 */
var ODS = {};
(function make_ods(ODS) {
/* Open Document Format for Office Applications (OpenDocument) Version 1.2 */
var get_utils = function() {
    if(typeof XLSX !== 'undefined') return XLSX.utils;
    if(typeof module !== "undefined" && typeof require !== 'undefined') try {
        return require('../' + 'xlsx').utils;
    } catch(e) {
        try { return require('./' + 'xlsx').utils; }
        catch(ee) { return require('xl' + 'sx').utils; }
    }
    throw new Error("Cannot find XLSX utils");
};

So I have tried some configurations to overcome this error but not able rectify this error. Is there any fix for this error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source