Reputation: 15094
As per understanding , Node js uses CommonJS module pattern , and in CommonJS pattern we use require() to import a node module.
In Angular 2 application development we use @angular/core , @angular/common etc node modules.
My question is:
Why do we use "import {} from '@angular/core'"[which is ES6 module syntax] instead of commonJS require() syntax for accessing node modules in angular2 code files.
Upvotes: 4
Views: 791
Reputation: 3565
The reason for this, is that Angular2 is written in TypeScript.
TypeScript is a superset of ES2015, and wants to be as close to ES2015 suggested syntax as possible. That's why you use the ES2015 import {} from syntax.
However, TypeScript also comes with a built-in transpiler (tsc). Meaning you write TypeScript code, but target a specific EcmaScript version in your tsconfig.json
Cheers
Upvotes: 4