问题
I've been following angular's documentation on how to use webpack 2 with angular 2. My code (github src here) is set up up with the webpack.dev.js
scenario.
Running the dev build using npm start
(i.e., webpack-dev-server --inline --progress --port 8080
) gives a series of TS2304
errors, such as
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'.
What is going wrong?
回答1:
make sure you have installed @types/node
.
then write "types": ["jasmine","node"]
in tsconfig.json
file
then this error will remove.
because same error is also facing by me. this solution is worked for me.
my 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"]
}
}
and 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"
}
}
回答2:
You need Node.js typings.
npm install @types/node --save-dev
回答3:
Adding this to my tsconfig.json fixed it for me:
"typeRoots": [
"../node_modules/@types/"
]
I ran into the issue following the Angular 2 Webpack Intro (https://angular.io/docs/ts/latest/guide/webpack.html)
The tsconfig.json code in the guide is different to the code in the 'final result' zip file (https://angular.io/resources/zips/webpack/webpack.zip)
Here's my whole 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/"
]
}
}
来源:https://stackoverflow.com/questions/42133190/ts2304-with-webpack-2-and-angular-2