pdfkit: Can't resolve 'fs' in .../...node_modules\pdfkit\js' - webpack loader 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?
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 11
- Comments: 16 (1 by maintainers)
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.
including the browser releases for pdfkit and blobstream worked for me https://github.com/devongovett/pdfkit/releases https://github.com/devongovett/blob-stream/releases
since they don’t rely on fs
I ran into the same problem and adding these 2 loaders into
webpack’s config solved the problem. Basically they makewebpackrun allpdfkit-related packages throughbrowserifywith thebrfstransform (by invoking thetransform-loader) and also inline the./data.jsonfile insideunicode-properties(one ofpdfkit’s dependencies) (invoking thejson-loader). Note that you also need to install thebrfsmodule.I didn’t have to set
{node: {fs: 'empty'}}.@dicbrus i used jsPdf but is not that powerful. Adding:
to my
webpack.config.jsDidn’t work either for me - but do try because lot’s of other people say it works…For the record I summarize what did the trick for me. Many thanks to @huy-nguyen for the solution and @kelly-tock for the tip about the
brfsversion!I started my project with Vue.js starter project webpack.
In
package.jsonI changedwebpackversion to 3:Naturally, I added
pdfkitto mydependencies:Then, I added the following
devDependencies:Finally, I fixed the webpack config in the file
build/webpack.base.conf.jsby adding the following rules tomodule.rulesarray:In one of my source files I included PDFKit using
require:Finally my project built and let me use PDFKit without dirty workarounds! 😃