Read and write a text file in typescript

前端 未结 4 2089
暖寄归人
暖寄归人 2020-12-10 00:39

How should I read and write a text file from typescript in node.js? I am not sure would read/write a file be sandboxed in node.js, if not, i believe there should be a way i

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 01:03

    believe there should be a way in accessing file system.

    Include node.d.ts using npm i @types/node. And then create a new tsconfig.json file (npx tsc --init) and create a .ts file as followed:

    import fs from 'fs';
    fs.readFileSync('foo.txt','utf8');
    

    You can use other functions in fs as well : https://nodejs.org/api/fs.html

    More

    Node quick start : https://basarat.gitbooks.io/typescript/content/docs/node/nodejs.html

提交回复
热议问题