@types/node installed typescript version not able

2020-02-29 02:07发布

问题:

I found typescript version for node unfortunately it's not able find "child_process" module from the node packages.

Exactly the error message in my console is Module not found: Error: Can't resolve 'child_process'

When I went through some blog they recommended to add below code in your tsconfig.json file.

"typeRoots": [
      "node_modules/@types"
    ]

Same thing as I pasted over here unlikely it's did't help.

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "outDir": "./dist/out-tsc",
    "baseUrl": "src",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2016",
      "dom"
    ]
  }
}

Why they don't inject this node module ? Am I doing something wrong in tsconfig.json file? I would be a grateful if you could give a little help. Thanks in advance.

回答1:

Maybe is the path:

"typeRoots": [
      "../node_modules/@types"
    ]

Usually you have your tsconfig in a src.

Update:

My original answer makes no sense, because you have this "baseUrl": "src".

As instead, I believe it could be a Webpack issue.

Try adding this to your webpack.config file:

target: 'node'

Update 2:

Workaround: Add the following to your config file:

node: {
      child_process: 'empty'
}