I can't imagine any case in which you would use fs
inside a React component. Even though you can use React in the server to render stuff, the same code is supposed to run in the client, there's no way you can access fs
in the client.
If you want to use fs
in the server, this is an example:
import * as fs from 'fs';
import * as path from 'path';
fs.readFile(path.join(__dirname, '../../client/index.html'), 'utf8', (error, data) => {
// ...
})
On your package.json
file, make sure to have a dependency on node
"dependencies": {
"@types/node": "^7.0.5"
}
And this is how my tsconfig.json
file looks like:
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"allowJs": true,
"typeRoots": [
"./node_modules/@types"
]
},
"include": [
"./db/**/*",
"./src/**/*"
]
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…