question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

fs.exists is not a function in node env

See original GitHub issue

I am trying to use this package in node, not browser environment.

When I ran the code below, I got this error: TypeError: fs.exists is not a function (https://github.com/exceljs/exceljs/blob/76a205ced964e0a9c3f14addadb8156a442e45a5/lib/utils/utils.js#L129)

import * as Excel from "exceljs/dist/exceljs.js";

const filepath = 'C:\\Documents\\projects\\tmpls\\abcdedf.xlsx';
const workbook = new Excel.Workbook();
await workbook.xlsx.readFile(filepath).then(()=>{console.log(23456)}).catch((e)=>{console.log('[catch]', e)});

My webpack.config:

var path = require('path');
var webpack = require('webpack');

module.exports = {
  entry: {
    index: './src/core/index.js'
  },
  output: {
    path: path.join(__dirname, './dist'),
    filename: '[name].js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  target: 'node',
  module: {
    loaders: [
      {
        test: /\.js|jsx$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
}

Is there anything wrong with the way I use the package or my webpack.config?

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
shaoshcommented, Oct 27, 2019

Finally figured it out

webpack.config.js: need to set node_modules as externals

var path = require('path');
var webpack = require('webpack');
var nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: {
    index: './src/core/index.js'
  },
  output: {
    path: path.join(__dirname, './dist'),
    filename: '[name].js'
  },
  resolve: {
    extensions: ['.js', '.jsx']
  },
  target: 'node',
  externals: [nodeExternals()],
  module: {
    loaders: [
      {
        test: /\.js|jsx$/,
        loader: 'babel-loader',
        exclude: /node_modules/
      }
    ]
  }
}

.babelrc: actually not related to this issue, but just my own code using the await/async

{
  "presets": ["es2015", "stage-0", "stage-3"],
  "plugins": ["syntax-async-functions", "transform-regenerator", "transform-runtime"]
}

code:

import * as Excel from "exceljs";

const filepath = getTmplPath(user);
const workbook = new Excel.Workbook();
await workbook.xlsx.readFile(filepath).then(()=>{console.log(23456)}).catch((e)=>{console.log('[catch]', e)});
0reactions
shaoshcommented, Oct 27, 2019

@kigh-ota

I am using babel 6. This is my babelrc

{
  "presets": [
    "es2015",
    "stage-0"
  ],
  "plugins": ["transform-es2015-spread", "transform-object-rest-spread"]
}

Since I excluded the node_modules in the webpack, is babel still going to compile the exceljs?

I used the node v8.4.0 at the beginning. Then I upgraded to v12.13.0. It doesn’t fix the issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: fs.exists is not a function - Stack Overflow
I am having trouble with the fs module error in Node JS Application. I am using Webpack also,. My .js file: const fs...
Read more >
Node.js fs.exists() Method - GeeksforGeeks
The fs.exists() method is used to test whether the given path exists or not in the file system. Syntax: fs.exists( path, callback )....
Read more >
Process | Node.js v19.3.0 Documentation
The 'beforeExit' event is emitted when Node.js empties its event loop and has no additional work to schedule. Normally, the Node.js process will...
Read more >
Node.js — Check If a Path or File Exists - Future Studio
Asynchronously Check if a File Exists in Node. js. The fs module in Node. js comes with a deprecated exists method.
Read more >
What is the Node fs.exists() method? - Educative.io
The method returns a boolean value. exists() returns true if the file or path exists in the directory, and false if the file...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found