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.

Can't resolve 'fs' in .../...node_modules\pdfkit\js' - webpack loader issue

See original GitHub issue

My webpack.config.js looks like this:

module.exports = {
    // devtool: 'source-map',
    entry: './src/app.js',
    output: {
        path: path.resolve (__dirname, "dist"),
        filename: "bundle.js"
    },
    module : {
        rules : [
            { test: /\.js$/, loader: 'babel-loader' },
            {
                test : /\.html$/,
                use : [ 'html-loader' ]
            }
        ]
    },
    plugins:
        [ new HtmlWebpackPlugin ({
              template : `src/app.html`
          })
        ]
};

In my app.js i have just this line: - nothing else. const PDFDocument = require ('pdfkit'); Which gives me the following errors:

ERROR in ./~/pdfkit/js/document.js
Module not found: Error: Can't resolve 'fs' in 'E:\0000 Path..\node_modules\pdfkit\js'
 @ ./~/pdfkit/js/document.js 26:7-20
 @ ./src/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js

ERROR in ./~/pdfkit/js/image.js
Module not found: Error: Can't resolve 'fs' in 'E:\0000 Path...\node_modules\pdfkit\js'
 @ ./~/pdfkit/js/image.js 11:7-20
 @ ./~/pdfkit/js/mixins/images.js
 @ ./~/pdfkit/js/document.js
 @ ./src/app.js
 @ multi (webpack)-dev-server/client?http://localhost:8080 ./src/app.js

I can’t get this library working with webpack in any way. Please can you give me an example that uses webpack instead of broweseify?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:11
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

9reactions
matdoeringcommented, Jan 15, 2019

Does anyone know whether PDFKit is compatible with WebPack version 4? The previous solutions do not work anymore … I’m getting

Can't resolve 'fs'

for pdfkit/js/document.js, pdfkit/js/image.js, pdfkit/js/font/afm.js, png-js/png-node.js.

I have no clue what has changed from WebPack 3 to WebPack 4 … If anyone has got this working with WebPack 4.0, I’d very much appreciate it.

8reactions
huy-nguyencommented, Aug 10, 2017

I ran into the same problem and adding these 2 loaders into webpack’s config solved the problem. Basically they make webpack run all pdfkit-related packages through browserify with the brfs transform (by invoking the transform-loader) and also inline the ./data.json file inside unicode-properties (one of pdfkit’s dependencies) (invoking the json-loader). Note that you also need to install the brfs module.

I didn’t have to set {node: {fs: 'empty'}}.

{
  module: {
    rules: [
      {
          test: /node_modules\/(pdfkit|fontkit|png-js|linebreak|unicode-properties|brotli)\//,
          loader: "transform-loader?brfs"
      },
      {
        test: /node_modules\/unicode-properties.*\.json$/,
        use: 'json-loader',
      },
    ]
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Module not found: Error: Can't resolve 'fs' in - Stack Overflow
I found a possible solution, that you have to put some configuration in one of the node_modules. But I think that is not...
Read more >
can't resolve fs in node_modules - You.com | The AI Search ...
Your quick fix is to take react scripts down to v4 until a fix for v5 is in place unless you are comfortable...
Read more >
pdfkit - Bountysource
Can't resolve 'fs' in .../...node_modules \pdfkit\js' - webpack loader issue ... My webpack.config.js looks like this: module.exports = { // ...
Read more >
Module not found: Error: Can't resolve 'fs' - Laracasts
[01:02:59] Starting 'webpack'... { [Error: ./resources/assets/js/router.js Module not found: Error: Can't resolve 'fs' in ...
Read more >
How To Solve Module Not Found Can't Resolve 'fs' in Next.js
The Module not found: Can't resolve 'fs' error and similar issues most likely occur when you try to import a module that is...
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