0

I followed the Angular 2 quickstart guide, which worked fine. But I reaaallly dislike the ts compiler they use and also don't want billions of dependencies I don't need in my node_modules folder. So I decided I was gonna build my own angular 2 project from scratch using gulp for the compiling/uglifying/concatting of my typescript code into 1 file.

Now when I run my application I see "Loading..." instead of "Hello world!".

And in my console I get the following errors:

GET http://localhost:3000/app/main.js 404 (Not Found) (zone.js:138)

Error: (SystemJS) XHR error (404 Not Found) loading ((index):37)

Im guessing something is going wrong here with my SystemJS and/or the compiling of typescript. It is looking for a 'main.js' even though I only have a 'main.ts'.

In the quickstart guide there was indeed a 'main.js' in my app folder, as a result of the compiled typescript. But since I'm using gulp, all my compiled stuff goes into a separate folder, with a separate name, so I have no 'main.js'.

At this point I can't find any solutions online and I'm already starting to miss Angular 1, so any help would be more than welcome.

I would be happy to provide any of my code if needed, but my app folder, systemjs.config.js and tsconfig.json are exactly the same as the quickstart guide.

UPDATE

This is what my systemjs.config.js looks like, as I expect that's where the problem lies. It seems to be looking for .module and .component files, but I only have 1 file: 'app.min.js' that contains all the code. Is this wrong?

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/'
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'dist/app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/router/upgrade': 'npm:@angular/router/bundles/router-upgrade.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',
      '@angular/upgrade/static': 'npm:@angular/upgrade/bundles/upgrade-static.umd.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api/bundles/in-memory-web-api.umd.js'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './app.min.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      }
    }
  });
})(this);
Max Taylor
  • 343
  • 3
  • 16

2 Answers2

1

Your problem isn't that you need to recreate you project with angular cli... Your project is saying that it cant find your main.js file or in other words your main.ts file. In angular 2 your compiler npm takes those .ts files and converts them into .js or javascript files. So the error you had

GET http://localhost:3000/app/main.js 404 (Not Found) (zone.js:138)

is basically saying it cant reference your main.ts file. a few things you could try would be

  1. Is your main.ts file actually located within your app folder? Based on your error that is where you project believes it needs to be.
  2. What is your index.html looking for when you start your application? for example you should have a script that looks something like this...

<script> System.import('app').catch(function(err){ console.error(err) }); </script>

The important part to note is the ('app') part. this is the file path that will be referenced to go find your main.ts

3.Make sure that ('app') is actually referencing the correct path location to you main.ts. To find out simply navigate to your systemjs.config.js file and look for something like this...

map: {
  app: 'app/components',

Simply change the path of the app to where every you have placed you main.ts file. In this example I would have to have my main.ts located under my components folder.

Edit

ok this is my last edit:

it is important that you understand that your typescript is not whats actually being compiled, if you have split your .js files into another folder, then that is what you need to be pointing at for your app path definition. I also wanted to add this as a note. Don't be afraid of npm and node_modules. angular 2 is bulky only in development when it comes to actually deploying your project, angular will shave your project to and extremely reasonable size you can reference other questions on SO found here

Community
  • 1
  • 1
Bean0341
  • 1,632
  • 2
  • 17
  • 37
  • Thanks for your answer! I believe you are right that the problem lies within my systemjs.config.js file. I tried adding the map part, but understood it wrong. I changed it to match my appname and made it reference my concatted file: 'app.min.js', Now the errors Im getting are 404 cant find errors. It's as if systemjs or zone.js is looking for app.component and app.module files. Is it a problem that I don't have these? Since I only have 1 bundled file. I'll edit my post to show you what my config file looks like – Max Taylor Nov 21 '16 at 17:53
  • It is worth noting that you will continue to have problems as long as you are separating out your .ts and js files. continue remapping all your errors to point at the folder in which you have your .js files. because those are what really matter when compiling. typescript is simply for ease of user or what they believe to be a "simpler language" – Bean0341 Nov 21 '16 at 17:59
  • So I need to have the .js files present in my project? Because currently Im compiling my typescript to javascript, then concatting and uglifying it in 1 go, meaning I never have the separate .js files in my project. Just .ts files and one app.min.js – Max Taylor Nov 21 '16 at 18:13
  • yes I believe you do. because the .js is the actual running code. However; There are ways of hiding them in your project so that way you dont have 3 files per component and adding confusion while you are trying to code (.ts, .js. and I believe .map also is present). you can find help on hiding those files, just depends on what editor you are using. you can find visual studios code for example here http://stackoverflow.com/questions/31587949/hide-js-map-files-in-visual-studio-code – Bean0341 Nov 21 '16 at 18:18
  • and just for clarification this is just "hiding" the files not removing or separating them from your project because you NEED them. :) – Bean0341 Nov 21 '16 at 18:22
  • Hmm okay, however: uglifiying and concatting should not affect the code right? So all the code is actually there, and it's javascript. It's just that zone.js (I think, might be something else) is looking for app.module.js and app.component.js files. The module and component code is in my app.min.js, but it doesnt seem to acces it – Max Taylor Nov 21 '16 at 18:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/128642/discussion-between-bean0341-and-max-taylor). – Bean0341 Nov 21 '16 at 18:30
0

If you want to start a new project, here's what you should do :


Install angular-cli :

npm i -g angular-cli

Create a new project :

ng new yourNewProjectName --style=scss

(You can remove scss or replace it by either "less" or "sass". Default : "css").


Go to your project :

cd yourNewProjectName

Start the project :

ng serve

Go to http://localhost:4200


Happy play with angular2 !

maxime1992
  • 22,502
  • 10
  • 80
  • 121
  • I guess I'm forced to use a pre-build project structure then... I'll just use this and manually remove everything I don't need... My goal was to understand why it wasnt working tho, not to fix it by using a scaffolding tool. Anyway thanks for your answer! – Max Taylor Nov 21 '16 at 14:50
  • systemjs is no longer used by angular-cli because it's way more complicated to add 3rd part libs for example. When I saw "At this point I can't find any solutions online and I'm already starting to miss Angular 1" I thought you wanted to start a project quickly and focus on NG2 code, not on build system. Good luck with ng2 anyway :) – maxime1992 Nov 21 '16 at 15:11
  • This isn't an appropriate way to answer this question. The angular 2 quickstart is a great way to start learning angular 2 and his problem is probably just a folder structure issue. – Bean0341 Nov 21 '16 at 15:46
  • 1
    @Bean0341 if he wants to focus on how to transpile TS, SCSS, etc ok, he can do all of that. But at the end of the tutorial, he'll find something like : "anyway we dropped systemjs in angular-cli (which is the official cli) so now just use webpack and forgot what we said with systemjs". It seems to me that he wanted to focus on Angular2 and not how to setup a project. Maybe I was wrong, therefore your answer is better idea ;) – maxime1992 Nov 21 '16 at 15:59
  • @Maxime Sorry for making you misunderstand! I was planning on switching to Webpack after I grasped the basic understanding off angular2. I just wanted to create a angular 2 skeleton based on my personal preferences, but I wanted to concat and create a web server with gulp. I read somewhere webpack could do this too, so I wanted to try it like this first. I think the problem is that the systemjs.config.js is still based on a project that will have both the typescript and javascript files in the same folder after compiling my TS to JS. – Max Taylor Nov 21 '16 at 17:43