简体   繁体   中英

New Angular project won't compile with strict mode

Generated Angular project with CLI Modified tsconfig , added strict options

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

When running ng serve I get following error:

ERROR in src/app/app.component.ts(2,29): error TS7016: Could not find a declaration file for module 'util'. 'D:/proj/node_modules/util/util.js' implicitly has an 'any' type. Try npm install @types/util if it exists or add a new declaration (.d.ts) file containing declare module 'util';

I tried npm install suggested - this module does not exist. I also tried to delete node_modules and do npm install . Same thing. Any pointers?

EDIT

app.component.ts

import { Component, OnInit } from "@angular/core";
import { isUndefined } from "util";

@Component({
  selector: "app-root",
  templateUrl: "./app.component.html",
  styleUrls: ["./app.component.scss"]
})
export class AppComponent {
  title = "Portal";
}

try this:

1: Delete node_modules

2: Run npm install

3: Run ng serve --aot=false

Hope it will help you.

Just comment out or remove the following line from main.ts (added by the upgrade process):

export { renderModule, renderModuleFactory } from '@angular/platform-server';

might be resolved your problem

Declare variables before assigning any values in strict mode:

var title = "portal"; 

or

let title = "portal"

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