Akanksha Akanksha - 1 month ago 26
TypeScript Question

Angular 2 - moved to @types giving error "cannot find name 'require'"

I have moved my project from old typings to types-publisher. Most of my dependencies are resolved except I keep getting this error Cannot find name 'require'
here is my tsconfig.json

{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"removeComments": false,
"noImplicitAny": false
},
"typeRoots": [
"./node_modules/@types",
"./src/custom-typings.d.ts"
],
"types": [
"core-js",
"jasmine",
"node",
"lodash",
"jquery",
"requirejs",
"angular",
"googlemaps"
]
}


in my package.json for "devdependencies", I have these

"@types/angular": "^1.5.16",
"@types/core-js": "^0.9.34",
"@types/googlemaps": "^3.25.35",
"@types/jasmine": "^2.5.35",
"@types/jquery": "^2.0.33",
"@types/lodash": "^4.14.37",
"@types/node": "^6.0.45",
"@types/requirejs": "^2.1.28",


I installed @types/requirejs too... but this error won't go away.
Is there something I am missing or adding incorrectly?

I am using webpack 2.1.0.beta.25 for my application.

Answer

Change your tsconfig.json as follows as typeRoots and types are compilerOptions properties as per documentation :

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "removeComments": false,
    "noImplicitAny": false,
    "types": [
      "core-js",
      "jasmine",
      "node",
      "lodash",
      "jquery",
      "requirejs",
      "angular",
      "googlemaps"
    ],
    "typeRoots": [
      "./node_modules/@types",
      "./src/custom-typings.d.ts"
    ]
  }
}

Also check documentation for types, typeroots and types which says that If typesRoots is specified, only packages under typeRoots will be included so check by removing typesRoots or making change in typesRoots property settings.

Comments