【问题标题】:Angular 4: "Cannot find name 'require'Angular 4:“找不到名称‘require’
【发布时间】:2017-09-14 12:38:16
【问题描述】:

我正在使用 Angular 4webpack 构建一个应用程序。

我的一个组件中有以下内容:

ngOnInit() {
    require('/assets/js/regular-expresions.js');
}

当我尝试编译时,我得到:

错误 C:/SRC/Sandbox/eat-sleep-code.com/src/app/content.component.ts (21,9): 找不到名称“需要”。

我已经运行 npm install @types/node --save-dev 并将我的 tsconfig.json 更新为如下所示:

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

但是,同样的错误不断被抛出。有什么想法吗?


这是我的 package.json:

{
  "name": "eat-sleep-code.com",
  "version": "0.0.0",
  "license": "MIT",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/animations": "^4.0.1",
    "@angular/common": "^4.0.0",
    "@angular/compiler": "^4.0.0",
    "@angular/core": "^4.0.0",
    "@angular/forms": "^4.0.0",
    "@angular/http": "^4.0.0",
    "@angular/material": "^2.0.0-beta.3",
    "@angular/platform-browser": "^4.0.0",
    "@angular/platform-browser-dynamic": "^4.0.0",
    "@angular/router": "^4.0.0",
    "@types/node": "^6.0.69",
    "angularfire2": "^2.0.0-beta.8",
    "core-js": "^2.4.1",
    "firebase": "^3.7.8",
    "jquery": "^3.2.1",
    "jquery-validation": "^1.16.0",
    "ng2-gist": "^1.0.0",
    "promise-polyfill": "^6.0.2",
    "requirejs": "^2.3.3",
    "rxjs": "^5.1.0",
    "typescript": "^2.2.2",
    "web-animations-js": "^2.2.4",
    "webpack": "^2.4.1",
    "zone.js": "^0.8.4"
  },
  "devDependencies": {
    "@angular/cli": "1.0.0",
    "@angular/compiler-cli": "^4.0.0",
    "@types/jasmine": "2.5.38",
    "@types/node": "^6.0.69",
    "codelyzer": "~2.0.0",
    "jasmine-core": "~2.5.2",
    "jasmine-spec-reporter": "~3.2.0",
    "karma": "~1.4.1",
    "karma-chrome-launcher": "~2.0.0",
    "karma-cli": "~1.0.1",
    "karma-coverage-istanbul-reporter": "^0.2.0",
    "karma-jasmine": "~1.1.0",
    "karma-jasmine-html-reporter": "^0.2.2",
    "protractor": "~5.1.0",
    "ts-node": "~2.0.0",
    "tslint": "~4.5.0",
    "typescript": "^2.2.2",
    "webpack": "^2.4.1"
  }
}

【问题讨论】:

  • 尝试从tsconfig中删除typestypeRoots
  • 尝试使用导入。在此处查看将 Webpack 与 Angular 结合使用的文档:angular.io/docs/ts/latest/guide/webpack.html
  • @DeborahK,据我了解,他不想让 TS 处理require,他把它留给webpack
  • @DeborahK 我得到“导入声明只能在命名空间或模块中使用”,因为这是在 ngOnInit 中
  • @Maximus 删除 typestypeRoots 无效。同样的“找不到名称'require'”错误。

标签: angular webpack


【解决方案1】:

尝试在顶部添加declare var require: any;

【讨论】:

  • 你是我的英雄!
  • 这个!这就是我一直在寻找的
  • 这应该是答案。
  • @zixia 这是一个黑客。
  • @saurabh 正确配置 typescript require 将有一个类型。将 require 声明为 any 类型并不能解决实际问题。
【解决方案2】:

require() 是不可取的,原因有几个。一,它会“破坏” webpack 2 中 tree-shaking 的有效性,二,它会阻止正确的代码拆分。

更好的选择是使用 webpack 的 System.import 实现:

System.import('/assets/js/regular-expresions.js').then(file => {
   // perform additional interactions after the resource has been asynchronously fetched
});

为了满足 Typescript 编译器(如您所见),您需要通过声明 System 来告诉它是可接受的变量:

declare var System: any;    

如果您只打算引用一次,则可以将此行添加到组件中,也可以将其添加到 typings.d.ts 类型定义文件中,以便在整个应用程序中使用它。请注意,如果您更改类型文件,则需要重新启动 ng serve 会话。

这在构建时满足编译器的要求,并在运行时被构建系统劫持,将调用修补到 Webpack 的 System.import

Webpack 反过来会检查它是否有一个块满足请求的文件,如果没有,它会动态创建一个脚本标签并将其插入头部,加载带有async 属性的文件,这就是在这种情况下发生的情况。

一旦文件加载,它将被解析并执行。但是,如果您想通过 async .then() 回调与它的任何部分进行交互,您的文件将需要导出您希望调用的任何内容,例如:

export function test() {
    console.log('we called test');
}

这会将.test() 函数公开给您的回调处理程序:

System.import('/assets/js/regular-expresions.js').then(file => {
   file.test();
});

【讨论】:

  • 小问题,我无法让 System.import 使用变量。我正在使用这种语法System.import(``assets/js/${script}``};,但它不起作用。如果我将其硬编码为System.import(``assets/js/regular-expressions.js``};,它会很好地加载文件。
  • 这确实是一个单独的问题,但很可能是您如何创建字符串的问题。如果您手动连接问题会消失吗?
  • 我打开了一个单独的问题:stackoverflow.com/questions/43507816/…
  • 如果我将JS库导入我的项目呢?
  • @tjvg1991 我不确定你想问什么。 ES6 导入几乎总是首选,但这不是这个答案的主题。
【解决方案3】:

尝试这些步骤来修复错误

  1. npm install --save-dev @types/webpack-env

  2. 在 compilerOptions 下使用以下内容更新您的 tsconfig.json:

    "types": [
        "webpack-env"
    ]
    

3.现在去 tsconfig.app.json 添加这个

"compilerOptions": {
 
    "types": [
      "node"
    ]
   }

【讨论】:

    【解决方案4】:

    试试下面,

    <any>require('/assets/js/regular-expresions.js')
    

    希望这会有所帮助!

    【讨论】:

    • 有了这个我仍然得到'找不到名称'require'。错误
    【解决方案5】:

    试试

    compilerOptions: {
       ...
       "typeRoots": [ "../node_modules/@types" ],
       ...
    }
    

    在 tsconfig.json 中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-30
      • 1970-01-01
      • 2018-02-02
      • 2019-04-06
      • 1970-01-01
      • 2017-09-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多