9

I generated a new Angular 2 project using the new Angular CLI. Now I don't want to use Angular in browser but in an electron app. Therefore I created a file for the main process of electron and after building my Angular app with ng build in terminal, the app was not working as expected.

File for Main Process of Electron:

var electron = require("electron");
var {app, BrowserWindow} = electron;

app.on('ready', () => {
    var mainWindow = new BrowserWindow();
    mainWindow.loadURL(`file://${__dirname}/dist/index.html`);
});

Error in DevTools:

file:///vendor/es6-shim/es6-shim.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/reflect-metadata/Reflect.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/systemjs/dist/system.src.js Failed to load resource: net::ERR_FILE_NOT_FOUND
file:///vendor/zone.js/dist/zone.js Failed to load resource: net::ERR_FILE_NOT_FOUND
index.html:22 Uncaught ReferenceError: System is not defined

I also know that you essentially need a web server to run Angular 2 Applications and I think my app does not work because Electron loads the App via the file:// protocol.

But I really want to use Electron in conjunction with Angular 2; so my question is if this is possible and if so how I need to modify the generated Angular Template from the cli utility.

3
  • 1
    First search result auth0.com/blog/2015/12/15/… Commented Jul 7, 2016 at 18:06
  • Yes, but I would like to know how to modify the template generated by the Angular CLI. Commented Jul 7, 2016 at 18:09
  • Just change the base href from href="/" to href="./" which will allow the app to run Commented May 5, 2018 at 4:01

4 Answers 4

12

You can also change

<base href="/">

to

<base href="./">
Sign up to request clarification or add additional context in comments.

Comments

6

After reading something about the Angular 2 Router yesterday, I figured out that the problem is caused by this line in the index.html

<base href="/">

By replacing it with a dynamically set base everything works as expected.

<script>document.write('<base href="' + document.location + '" />');</script>

Comments

2

This Github project by 'maximegris' sets up the environment for working with Angular 5 and Electron.

https://github.com/maximegris/angular-electron

It proved to be a life saver for me. Hope you find this helpful too.

Comments

-2

download angular.js from the CDN and put it in your app folder. Then in your html file use

<script type="text/javascript" src="angular.js"></script>

Unlike a browser electron will not have a problem picking up angular.js from your file system.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.