node.js 在打字稿中使用 fs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43048113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Use fs in typescript
提问by Pierre Olivier Tran
I'm just trying to read a file using fs.readFileSync, though it seems it cannot be found.
我只是尝试使用 读取文件fs.readFileSync,尽管它似乎无法找到。
I made sure to declare it, added it within my constructor:
我确保声明它,将它添加到我的构造函数中:
export default class Login extends React.Component<LoginProps, {}> {
private webAuth: auth0.WebAuth;
fs: any;
constructor(props: any, context: any) {
super(props, context);
this.fs = require('fs');
this.webAuth = new auth0.WebAuth({
clientID: conf.auth0.clientId,
domain: conf.auth0.domain,
responseType: 'token id_token',
redirectUri: `${window.location.origin}/login`
});
}
[...]
And used it in a simple function:
并在一个简单的函数中使用它:
verifyToken = (token) => {
console.log(this.fs);
let contents = this.fs.readFileSync('../utils/public.key', 'utf8');
console.log(contents);
}
But this raises an Uncaught TypeError: _this.fs.readFileSync is not a function. Is there a special way to include fsin Typescript ?
但这会引发Uncaught TypeError: _this.fs.readFileSync is not a function. 有没有一种特殊的方法可以包含fs在 Typescript 中?
回答by André Pena
I can't imagine any case in which you would use fsinside 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 fsin the client.
我无法想象您会fs在 React 组件中使用的任何情况。即使你可以在服务器端使用 React 来渲染东西,同样的代码应该在客户端运行,你无法fs在客户端访问 。
If you want to use fsin the server, this is an example:
如果要fs在服务器中使用,这是一个示例:
import * as fs from 'fs';
import * as path from 'path';
fs.readFile(path.join(__dirname, '../../client/index.html'), 'utf8', (error, data) => {
// ...
})
On your package.jsonfile, make sure to have a dependency on node
在您的package.json文件中,确保依赖于节点
"dependencies": {
"@types/node": "^7.0.5"
}
And this is how my tsconfig.jsonfile looks like:
这就是我的tsconfig.json文件的样子:
{
"compilerOptions": {
"outDir": "./dist/",
"sourceMap": true,
"noImplicitAny": true,
"module": "commonjs",
"target": "es5",
"jsx": "react",
"allowJs": true,
"typeRoots": [
"./node_modules/@types"
]
},
"include": [
"./db/**/*",
"./src/**/*"
]
}
回答by Shim-Sao
Using node -v 10.15.0and @types/node:
使用node -v 10.15.0和@types/node:
It seems declaration has been rewritten...
好像声明被改写了……
fsdefinition is declared as a moduleso you should do:
fs定义被声明为 amodule所以你应该这样做:
import fs from "fs"; // Without star
import fs from "fs"; // Without star
compiled:
编译:
var fs_1 = __importDefault(require("fs"));
var fs_1 = __importDefault(require("fs"));
or
或者
const fs = require("fs");instead of require("fs").default;
const fs = require("fs");代替 require("fs").default;
with star you will have fs.default.TheFunctionYouWantinstead of fs.TheFunctionYouWant
有了明星,你将拥有fs.default.TheFunctionYouWant而不是fs.TheFunctionYouWant
The better way is to console.log(fs);to see what it is imported.
更好的方法是console.log(fs);查看导入的内容。
{
"compilerOptions": {
"typeRoots": [],
"types": [
"node"
]
}
}

