2

I'm getting this error on vscode:

enter image description here

This is my package.json file:

{
  "name": "font-awesome-test",
  "version": "1.0.0",
  "description": "Test font-awesome with webpack",
  "main": "index.js",
  "repository": "git@github.com:jeusdi/fontawesome-webpack.git",
  "author": "Jordi Cabré",
  "license": "MIT",
  "dependencies": {
    "font-awesome": "^4.7.0",
    "typescript": "^2.7.1"
  },
  "devDependencies": {
    "webpack": "^3.10.0",
    "webpack-dev-server": "^2.11.1"
  }
}

Any ideas?

Jordi
  • 20,868
  • 39
  • 149
  • 333
  • BTW the text of the error message doesn't show up in the screenshot. – Robert Penner Feb 02 '18 at 17:11
  • Possible duplicate of [typescript getting error TS2304: cannot find name ' require'](https://stackoverflow.com/questions/31173738/typescript-getting-error-ts2304-cannot-find-name-require) – Heretic Monkey Feb 02 '18 at 17:23

3 Answers3

4

TypeScript can understand require after adding the Node type definitions:

npm install --save-dev @types/node

Shorthand:

npm i -D @types/node

Or with yarn:

yarn add -D @types/node

Also, you'll probably want to use

"moduleResolution": "Node",

in tsconfig.json to avoid different errors like

error TS2307: Cannot find module 'abc'
Robert Penner
  • 6,148
  • 1
  • 17
  • 18
4

I face the same error, when checked tsconfig.app.json file, it have:

"types": []

Just changed to

"types": ["node"]

And now everything is working smooth!

Ali Adravi
  • 21,707
  • 9
  • 87
  • 85
1

TypeScript supports two different module resolution strategies: Classic and Node. The default is Classic.

In order to use the require('package') method, you need to change your module resolution strategy to Node in the compileOptions of your tsconfig.json:

"compilerOptions": {
  "moduleResolution": "node",
}
Marc LaFleur
  • 31,987
  • 4
  • 37
  • 63
  • 1
    This addresses other errors like `TS2307: Cannot find module 'abc'` but it's not sufficient for the `require` error posted. – Robert Penner Feb 02 '18 at 17:08