0

Error:

src/app/app.component.ts:4:12 - error TS2591: Cannot find name 'module'. Do you need to install type definitions for node? Try npm i @types/node and then add node to the types field in your tsconfig.

4 moduleId: module.id,

When I try to run "ng build" in order to compile my angular files, I get error for every occurance of "module" var usage. I am sure the module is there, I can see it in node_modules folder and also I can ctrl+click on it and the file with module var would open.

I tried deleting whole node_modules dir and install again.

I left my tsconfig.json untouched since I am just learning so far, but here it is:

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

here is example of usage in ts component:

import { Component } from '@angular/core';

@Component({
    moduleId: module.id,
    selector: 'app',
    templateUrl: 'app.component.html'
})

export class AppComponent { }

Maybe it has something to do with my project structure, since I am not very sure what project structure to use. I am trying to build spring + angular aplication, with following structure:

structure

Structure of angular app should be alright since it is generated by ng new command

Output path in angular.json is set to "outputPath": "../public", which should be in resource dir.

Do you have any idea what could be wrong please?

Naxmars
  • 13
  • 6

2 Answers2

0

You can remove the module property in the component decorator. You no longer need to write @Component({ moduleId: module.id }), nor should you, module.id is automatically added by webpack. Maybe you are referring an old tutorial or code here, that is why module.id is still present here.

Refer this answer for more - Angular - What is the meanings of module.id in component?

abd995
  • 1,649
  • 6
  • 13
  • Thank you very much, I did not knew that, I removed module.id and it works just fine. I also found other workaround I posted here, but this seems like the right solution. – Naxmars Jul 09 '19 at 12:05
0

I fixed this by removing following things from tsconfig.app.json:

"compilerOptions": {
  "outDir": "./out-tsc/app",
  "types": []
},

and adding this into tsconfig.json under compilerOptions

"types": ["node"]

Seems like compiler needs this "types": ["node"] option in order to find the module, and also it seems that by default this option is overriden in tsconfig.app.json..

I will not close this question a few days in case someone have a good explanation for me :-)

//EDIT: There is much better solution for this, I checked the answer as right.

Thank you

Naxmars
  • 13
  • 6