简体   繁体   English

TS2304:找不到名称'require'

[英]TS2304: Cannot find name 'require'

Webpack (specifically awesome-typescript-loader ) fails to build my app because of the following error: 由于以下错误,Webpack(特别是awesome- typescript -loader )无法构建我的应用程序:

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 1.类型安装在package.json

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

2. awesome-typescript-loader is aware of my tsconfig.json : 2. awesome-typescript-loader tsconfig.json awesome-typescript-loader知道我的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 3. Types以及type roots已在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? 还有什么我可以尝试的吗?

我通过添加一个新的webpack.d.ts并在其中添加以下内容来解决这个问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM