Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2 & TypeScript importing of node_modules

I had a very simple 'hello world' Angular2 app. I also made the apparently unreasonable decision to work with different directory structures between my dev project and the final deployment folder on my spring backend.

Because of this difference, I had an issue with the TypeScript imports, and this line ended up producing 404 errors (unable to find /angular2/core library) when I tried to open the actual app in my browser:

import {Component, View} from 'angular2/core';

So long story short, I ended up adding back the /app folder to make everything work, but I ended up modifying my import statements as follows:

import {Component, View} from '../node_modules/angular2/core';

This, however, turned out to cause some weird behavior. For some reason specifying ../node_modules in the library paths is causing the JS to actually load ALL Angular2 files from scratch using ajax calls to retrieve each and every individual file from the npm_modules/angular2/ folder even though this was part of my HTML header:

<script src="/node_modules/angular2/bundles/angular2.dev.js"></script>

When I finally realized what's going on I reverted the import statement back to

import {Component, View} from 'angular2/core';

and it all worked. Angular2 was now completely loaded from the script tag above and there were no files getting loaded by extra ajax calls.

Can someone please explain what is causing this? I assume it's normal behavior but I don't understand how the importing works and why specifying a more detailed path makes such a difference.

like image 803
RVP Avatar asked Feb 02 '16 17:02

RVP


People also ask

Should I learn AngularJS or Angular 2?

Angular 2 is more useful for developing mobile applications and includes higher performance speeds than AngularJS. JavaScript is easier to understand than TypeScript, making Angular 2 a more advanced and challenging framework for developers to use.

Is Angular 2 better than react?

The difference between Angular and React. js is that Angular 2 uses both one- and two-way data binding: changing data impacts view and changing view triggers changes in data. React uses one-way binding: when designing a React app developers often nest child components within higher-order parent components.

How old is Angular 2?

Angular 2 moved to Beta in December 2015, and the first release candidate was published in May 2016. The final version was released on September 14, 2016.

What is Angular 2 architecture?

Each Angular 2 application needs to have one Angular Root Module. Each Angular Root module can then have multiple components to separate the functionality. Following is an example of a root module. Each application is made up of feature modules where each module has a separate feature of the application.


1 Answers

The import rules of TypeScript follow the same convention as node.js. If an import begins with a dot:

import {Something} from './some/path';

Then it is treated as a relative path from the file that declares the import. If however it is an absolute path:

import {Component} from 'angular2/core';

Then it is assumed to be an external module, so Typescript will walk up the tree looking for a package.json file, then go into the node_modules folder, and find a folder with the same name as the import, then looks in the package.json of the module for the main .d.ts or .ts file, and then loads that, or will look for a file that has the same name as the one specified, or an index.d.ts or index.ts file.

Wow that seems complex when written out, and there are still some exceptions there... But all in all, if you have worked with node.js before then this should behave exactly the same way.

One thing to note is that there is a TypeScript compiler option that should be set for typing resolutions to work in this way

in tsconfig.json

"moduleResolution": "node"

Now the second part of your question was how does this get loaded without using ajax calls. This is a feature of System.js. The script tag that is loaded in the index.html file imports a bundle which registers the angular2 bundle with System. Once this has happened System knows about these files and correctly assigns them to their references. It's a pretty deep topic but a lot of information can be found either in the README of systemjs, or systemjs-builder.

like image 122
SnareChops Avatar answered Oct 13 '22 19:10

SnareChops