angular / angular-cli

CLI tool for Angular
https://cli.angular.io
MIT License
26.73k stars 11.98k forks source link

Can't import node core modules( fs, os, child_process, etc.) in Angular projects. #9871

Closed aifer2007 closed 6 years ago

aifer2007 commented 6 years ago

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[ ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead see https://github.com/angular/angular/blob/master/CONTRIBUTING.md#question

Current behavior

I use node core modules (os, fs, path, child_process, dgram, etc.) in my Angular4 project. I create a new angular4 project:


ng new ngs
cd ngs
ng serve --open
...
webpack: Compiled successfully.

Then I try to import 'os' module in app.component.ts file


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

// to import node core modules
import { CpuInfo } from 'os'

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

  constructor(cpu:CpuInfo){

  }
}

and then


ng serve --open

Some errors are reported:


ERROR in src/app/app.component.ts(4,25): error TS2307: Cannot find module 'os'.
webpack: Failed to compile.

All the node core modules, such as fs, os, child_process, dgram etc., will give the same error


Cannot find module 'core-module-name'.

Yet, much extenral node modules work well at the same time.

Expected behavior

How to use node core modules( fs, os, child_process, etc.) in Angular projects?

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Environment


Angular CLI: 1.7.2
Node: 8.9.4
OS: darwin x64
Angular: 5.2.7
... animations, common, compiler, compiler-cli, core, forms
... http, language-service, platform-browser
... platform-browser-dynamic, router

@angular/cli: 1.7.2
@angular-devkit/build-optimizer: 0.3.2
@angular-devkit/core: 0.3.2
@angular-devkit/schematics: 0.3.2
@ngtools/json-schema: 1.2.0
@ngtools/webpack: 1.10.1
@schematics/angular: 0.3.2
@schematics/package-update: 0.3.2
typescript: 2.5.3
webpack: 3.11.0



Browser:
- [x] Chrome (desktop) version XX
- [ ] Chrome (Android) version XX
- [ ] Chrome (iOS) version XX
- [ ] Firefox version XX
- [ ] Safari (desktop) version XX
- [ ] Safari (iOS) version XX
- [ ] IE version XX
- [ ] Edge version XX

For Tooling issues:
- Node version: 8.9.4  
- Platform:  Mac 

Others:

aifer2007 commented 6 years ago

For example, import a node core module 'os' and a normal module 'q' at the same time in app.component.ts:


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

// to import node core modules
import { CpuInfo } from 'os'

// to import common node modules
import { when } from 'q';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';

  constructor(cpu:CpuInfo){
    when(); // from 'q' module
  }
}

Then


 ng serve --open
...
ERROR in src/app/app.component.ts(4,25): error TS2307: Cannot find module 'os'.
webpack: Failed to compile.

ONLY 'os' reports error: Cannot find module 'os'. 'q' module works well.

clydin commented 6 years ago

Angular applications are web applications and run in a browser environment not a nodejs environment. As a result, nodejs builtins do not exist in that context.

angular-automatic-lock-bot[bot] commented 5 years ago

This issue has been automatically locked due to inactivity. Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.