I've encountered an issue that seems to be related to using NextJs with TypeScript.
For example:
// /pages/index.tsx
import _ from 'lodash'
export const MyComponent = () => {
return (
<ul>
{
_.map(someArray, el => <li>{el}</li>) // Error: Module not found: Can't resolve 'fs'
}
</ul>
)
}
This same error occurs with my custom functions as well, not just lodash functions.
When I import a function from a .ts file into my .tsx file and try to execute it within TSX, I receive a ModuleNotFound error. Sometimes I also encounter
ModuleNotFoundError: Module not found: Error: Can't resolve 'child_process'
. Interestingly, I can import and run a custom function imported from a .js file without any issues.
Here is my tsconfig.json configuration:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"...other options..."
},
"...other entries..."
}
Additionally, here is my package.json setup:
{
"dependencies": {
"@mdx-js/loader": "^1.6.22",
"...other dependencies..."
},
"devDependencies": {
"@types/lodash": "^4.14.172",
"...other devDependencies..."
}
}
In my next.config file, I use withMDX for handling MDX files:
const withMDX = require('@next/mdx')({
extension: /\.mdx$/
})
module.exports = withMDX({
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})
It seems like I may have overlooked something in configuring NextJs to work seamlessly with TSX and TypeScript. Any insights or solutions would be greatly appreciated!