Cannot find module 'fs'

前端 未结 4 1790
别那么骄傲
别那么骄傲 2021-01-11 12:20

Hello i\'m facing this issue here: Typescript Node.js simplest setup doesn't work -- error TS2307: Cannot find module 'fs'

And i\'ve tried also that sol

相关标签:
4条回答
  • 2021-01-11 13:09

    I know i'm probably late but i was faceing the same issue. Finally I got a work around if you want to try. In your tsconfig.json add "node_modules/@types/node" in "typeRoots" as you can see in the config bellow:

    {
      "compileOnSave": false,
      "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "module": "esnext",
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2015",
        "typeRoots": [
          "node_modules/@types",
          "node_modules/@types/node"
        ],
        "lib": [
          "es2018",
          "dom"
        ]
      }
    }
    
    0 讨论(0)
  • 2021-01-11 13:14

    Are you running this in Node?

    Why not import fs from fs?

    Also try require and see what's up

    0 讨论(0)
  • 2021-01-11 13:17

    I found the solution:

    import { writeFileSync, readFileSync } from 'fs';
    

    it works now

    0 讨论(0)
  • 2021-01-11 13:19

    This solved the issue for me:

    npm install @types/node
    
    0 讨论(0)
提交回复
热议问题