maelgrove
maelgrove

Reputation: 1009

TS2304: Cannot find name 'require'

Webpack (specifically awesome-typescript-loader) fails to build my app because of the following error:

ERROR in [at-loader] ./src/app/app.ts:6:13
   TS2304: Cannot find name 'require'.

The associated code:

@Component({
  name: 'App',
  template: require('./app.template.pug') <---
})
export class AppComponent extends Vue {

}

There's a similiar question here which describes the exact same error, and I've tried various solutions mentioned in the answers, however with no success:

1. Types are installed in package.json

"devDependencies": {
  " @types/node": "^7.0.22"
  ...

2. awesome-typescript-loader is aware of my tsconfig.json:

loader: 'awesome-typescript-loader',
options: {
  configFileName: 'tsconfig.json',
  useCache: config.build.useCaching
}

3. Types aswell as type roots have been declared in tsconfig.json

"typeRoots": [
  "node_modules/@types"
],
"types": [
  "node",
  "webpack"
],

4. My custom typings include definitions for node

declare module '*';
declare var require: any

Anything else I could try?

Upvotes: 3

Views: 6065

Answers (1)

harishr
harishr

Reputation: 18055

I had solve this by adding a new webpack.d.ts and adding below content to it..

        declare var require: (filename: string) => any;

Upvotes: 2

Related Questions