As far as I can tell, a custom <Document />
is only ever run in Node.js / SSR. If that's the case, why wouldn't importing native node modules be allowed?
edit: Is this because of the way javascript is compiled through webpack with emit-file-loader
?
import fs from 'fs';
in _document.js should compile
Build fails, suggesting installing fs
import fs from 'fs'
in a custom document.I'm working on flushing styled-components SSR output to a link tag, to enable potentially putting cloudfront in front of it and serve <link>
tags rather than inline styles.
| Tech | Version |
|---------|---------|
| next | 3beta |
Add a next.config.js
file to your root directory with the following content:
module.exports = {
webpack(config) {
config.externals = config.externals || {}
config.externals.fs = 'fs'
return config
},
}
Make sure to access fs
only server side:
if (typeof window === 'undefined') {
console.log(fs)
}
EDIT: I have removed the require('fs')
part. The import statement works fine.
Most helpful comment
Add a
next.config.js
file to your root directory with the following content:Make sure to access
fs
only server side:EDIT: I have removed the
require('fs')
part. The import statement works fine.