Import txt files as string into your next.js App
import file from './xxx.txt'
Webpack 5 has 'Asset Modules' to allow you to import assets without configuring loaders now.
Next.js use Webpack 5 internally, so we can use 'source assets' to import txt files as strings.
Two steps to let you be able to import a txt file as a string.
custom Webpack config for next.js
declare txt as a module for typescript
Wildcard module declarations can be used to cover these cases.
example
output
The full commit to enabling import txt files in your next.js app
references
https://webpack.js.org/guides/asset-modules/#source-assets
❤️ Support This Blog
If this post helped you, you can support my writing with a small donation. Thank you for reading.

Comments
Post a Comment