I have an issue which is likely related to using NextJs with typescript.
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>
}
I have the same error also for my own functions, not only with lodash functions.
If I import a function from a .ts file into my .tsx file and then execute it inside TSX, I get a ModuleNotFound error, also sometimes ModuleNotFoundError: Module not found: Error: Can't resolve 'child_process'
. I can however import and execute a custom function imported from a .js file.
This is my tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"strict": false,
"forceConsistentCasingInFileNames": true,
"noEmit": true,
"esModuleInterop": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"jsx": "preserve"
},
"include": [
"next-env.d.ts",
"**/*.ts",
"**/*.tsx"
],
"exclude": [
"node_modules"
]
}
package.json:
"dependencies": {
"@mdx-js/loader": "^1.6.22",
"@next/mdx": "^11.0.1",
"lodash": "^4.17.21",
"next": "^11.0.1",
"next-mdx-remote": "^3.0.4",
"react": "^17.0.2",
"react-dom": "^17.0.2",
"react-markdown": "^6.0.2",
"typescript": "^4.3.5"
},
"devDependencies": {
"@types/lodash": "^4.14.172",
"@types/react": "^17.0.20",
"@typescript-eslint/eslint-plugin": "^4.30.0",
"eslint": "^7.32.0",
"eslint-config-next": "^11.1.2"
}
}
next.config:
const withMDX = require('@next/mdx')({
extension: /\.mdx$/
})
module.exports = withMDX({
pageExtensions: ['js', 'jsx', 'ts', 'tsx', 'md', 'mdx'],
})
I guess I missed something when configuration NextJs to work with TSX and Typescript. Thank you!