Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compilation error : Cannot find module 'util'

I'm new programmer on angular2. I have problem when compile with angular cli.

I generate angular 2 project by angular cli. I'm trying to use debuglog module on node_modules/@types/node by import module like this: import {debuglog} from "util"; I use Webstorm IDE for develop and the ide do not warning or error with my import. I checked and found debuglog module in path: myproject/node_modules/@types/node/index.d.ts.

What's wrong in my code? Have any suggestion for fix it?

Project on Github: https://github.com/sinhpn92/angular2-example-types-node

Version of environments:

@angular/cli: 1.0.0-rc.1
node: 7.7.1
os: darwin x64
@angular/common: 2.4.9
@angular/compiler: 2.4.9
@angular/core: 2.4.9
@angular/forms: 2.4.9
@angular/http: 2.4.9
@angular/platform-browser: 2.4.9
@angular/platform-browser-dynamic: 2.4.9
@angular/router: 3.4.9
@angular/upgrade: 2.2.4
@angular/cli: 1.0.0-rc.1
@angular/compiler-cli: 2.4.9

enter image description here

like image 470
Phan Sinh Avatar asked Feb 04 '23 22:02

Phan Sinh


2 Answers

This solved the issue for me - https://github.com/angular/angular-cli/wiki/stories-third-party-lib

"types": [
  "node"
]

in tsconfig.app.json.

like image 151
Yuri Dolzhenko Avatar answered Feb 07 '23 13:02

Yuri Dolzhenko


Noticed that your tsconfig.json lacks the following entry in compilerOptions:

compilerOptions{
//......,
 "typeRoots": [
      "node_modules/@types"
    ]
 }

Try adding it.

like image 43
Suraj Rao Avatar answered Feb 07 '23 13:02

Suraj Rao