TS2304具有的WebPack 2和2角(TS2304 with Webpack 2 and An

2019-09-27 18:09发布

我一直在关注角度的文档关于如何使用的WebPack 2角2,我的代码(SRC的GitHub 这里 )建立了与webpack.dev.js场景。

使用运行开发建设npm start (即webpack-dev-server --inline --progress --port 8080 )给出了一系列的TS2304的错误,如

ERROR in [at-loader] src\app\app.component.ts:5:15
    TS2304: Cannot find name 'require'.

ERROR in [at-loader] src\app\app.component.ts:6:14
    TS2304: Cannot find name 'require'.

这是怎么回事了?

Answer 1:

请确保你已经安装了@types/node

然后写"types": ["jasmine","node"]tsconfig.json文件,则该错误将删除。

因为同样的错误也被我面对的问题。 该解决方案为我工作。

我tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "moduleResolution": "node",
    "sourceMap": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es2015", "dom"],
    "noImplicitAny": true,
    "suppressImplicitAnyIndexErrors": true,
    "types": ["jasmine","node"]
  }
}

和的package.json

{
  "name": "webpackCreator",
  "version": "1.0.0",
  "description": "A webpack starter for Angular",
  "scripts": {
    "start": "webpack-dev-server --inline --progress --port 8080",
    "test": "karma start",
    "build": "rimraf dist && webpack --config config/webpack.prod.js --progress --profile --bail"
  },
  "license": "MIT",
  "dependencies": {
    "@angular/common": "~2.4.0",
    "@angular/compiler": "~2.4.0",
    "@angular/core": "~2.4.0",
    "@angular/forms": "~2.4.0",
    "@angular/http": "~2.4.0",
    "@angular/platform-browser": "~2.4.0",
    "@angular/platform-browser-dynamic": "~2.4.0",
    "@angular/router": "~3.4.0",
    "core-js": "^2.4.1",
    "rxjs": "5.0.1",
    "zone.js": "^0.7.4"
  },
  "devDependencies": {
    "@types/jasmine": "^2.5.41",
    "@types/node": "^6.0.45",
    "angular2-template-loader": "^0.6.0",
    "awesome-typescript-loader": "^3.0.0-beta.18",
    "css-loader": "^0.26.1",
    "extract-text-webpack-plugin": "2.0.0-beta.5",
    "file-loader": "^0.9.0",
    "html-loader": "^0.4.3",
    "html-webpack-plugin": "^2.16.1",
    "jasmine-core": "^2.4.1",
    "karma": "^1.2.0",
    "karma-jasmine": "^1.0.2",
    "karma-phantomjs-launcher": "^1.0.2",
    "karma-sourcemap-loader": "^0.3.7",
    "karma-webpack": "^2.0.1",
    "null-loader": "^0.1.1",
    "phantomjs-prebuilt": "^2.1.7",
    "raw-loader": "^0.5.1",
    "rimraf": "^2.5.2",
    "style-loader": "^0.13.1",
    "typescript": "~2.0.10",
    "webpack": "2.2.0",
    "webpack-dev-server": "2.2.0-rc.0",
    "webpack-merge": "^2.4.0"
  }
}


Answer 2:

你需要Node.js的分型。

npm install @types/node --save-dev



Answer 3:

添加此我tsconfig.json固定对我来说:

"typeRoots": [ 
    "../node_modules/@types/"
]

我跑进了问题之后的角2的WebPack介绍( https://angular.io/docs/ts/latest/guide/webpack.html )

指南中的tsconfig.json码是在“最终结果” zip文件不同的代码( https://angular.io/resources/zips/webpack/webpack.zip )

这里是我的整个tsconfig.json:

{
    "compilerOptions": {
        "target": "es5",
        "module": "commonjs",
        "moduleResolution": "node",
        "sourceMap": true,
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "lib": [ "es2015", "dom" ],
        "noImplicitAny": true,
        "suppressImplicitAnyIndexErrors": true,
        "typeRoots": [ 
            "../node_modules/@types/"
        ]
    }
}


文章来源: TS2304 with Webpack 2 and Angular 2