简体   繁体   中英

Running “ng serve” with angular CLI first time wont compile right and gives an error

So I am trying to start my first project in angular.

And every time it won't compile like it won't do ng serve and complete it without an error, it is hard to explain I hope you forgive that with me on that.

Basically, Im running node-v9.1.0 and npm-v5.5.1 on Windows 10 Pro 64bit

Commands I used is npm install -g @angular/cli & ng new ngvic and the cd into the folder and using ng serve .

Then I get this error:

λ ng serve
** NG Live Development Server is listening on localhost:4200, open your browser on http://localhost:4200/ **
Date: 2017-11-21T09:53:09.965Z
Hash: 
Time: 2522ms
chunk {inline} inline.bundle.js (inline) 5.79 kB [entry] [rendered]
chunk {main} main.bundle.js (main) 3.49 kB [initial] [rendered]
chunk {polyfills} polyfills.bundle.js (polyfills) 1.19 kB [initial] [rendered]
chunk {styles} styles.bundle.js (styles) 9.02 kB [initial] [rendered]
chunk {vendor} vendor.bundle.js (vendor) 853 kB [initial] [rendered]

ERROR in C:/Users/USERNAME/Documents/sites/ngvic/src/main.ts
Module build failed: Error: C:\Users\USERNAME\Documents\sites\ngvic\src\main.ts is not part of the compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at AngularCompilerPlugin.getCompiledFile (C:\Users\USERNAME\Documents\sites\ngvic\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:619:23)
    at plugin.done.then (C:\Users\USERNAME\Documents\sites\ngvic\node_modules\@ngtools\webpack\src\loader.js:467:39)
    at <anonymous>
 @ multi webpack-dev-server/client?http://0.0.0.0:0 ./src/main.ts
ERROR in C:/Users/USERNAME/Documents/sites/ngvic/src/polyfills.ts
Module build failed: Error: C:\Users\USERNAME\Documents\sites\ngvic\src\polyfills.ts is not part of the compilation. Please make sure it is in your tsconfig via the 'files' or 'include' property.
    at AngularCompilerPlugin.getCompiledFile (C:\Users\USERNAME\Documents\sites\ngvic\node_modules\@ngtools\webpack\src\angular_compiler_plugin.js:619:23)
    at plugin.done.then (C:\Users\USERNAME\Documents\sites\ngvic\node_modules\@ngtools\webpack\src\loader.js:467:39)
    at <anonymous>
 @ multi ./src/polyfills.ts

webpack: Failed to compile.

And this is the ng -v

Angular CLI: 1.5.3
Node: 9.1.0
OS: win32 x64
Angular: 5.0.2
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.5.3
@angular-devkit/build-optimizer: 0.0.33
@angular-devkit/core: 0.0.20
@angular-devkit/schematics: 0.0.36
@ngtools/json-schema: 1.1.0
@ngtools/webpack: 1.8.3
@schematics/angular: 0.1.5
typescript: 2.4.2
webpack: 3.8.1

Hope anyone can help with this. Thanks in advance.

Edit: Added tsconfig.json

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

Edit 2: npm install warns

λ npm install
npm WARN codelyzer@3.2.2 requires a peer of @angular/compiler@^2.3.1 || >=4.0.0-beta <5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN codelyzer@3.2.2 requires a peer of @angular/core@^2.3.1 || >=4.0.0-beta <5.0.0 but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.1.3 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.1.3: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})

added 115 packages in 7.541s

add the main.ts file to the tsconfig.json under include property.

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

Try running npm install only in terminal. That fixed the issue for me during my first angular project

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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