Import JS file inside node_modules in Angular - javascript

I have an angular app, to this I am trying to add an npm package called svgedit. It gets installed. But when I try to reference a java-script file it says
net::ERR_ABORTED 404 (Not Found)
This is how I am trying to reference it in my index.html in the head section,
<script type="module" src="node_modules/svgedit/svgedit-config-es.js"></script>
I can access this file from within vs code by clicking "ctrl + left mouse". It opens the file properly meaning the reference is proper. But when it runs it gives the above mentioned error in the console. I have also tried switching the type attribute to "text/javascript".
Basically I am trying to integrate this package in my angular app. What is the correct method to go about it.

ng serve will only serve compiled files from your src directory and what''s in the assets folder. It will not serve node_module files directly.
The usual way to add scripts it to add it to the scripts section in angular.json
scripts:
[ //other scripts
"node_modules/svgedit/svgedit-config-es.js"
]

Is your html page in the root directory of your project? if not make sure to navigate to the file correctly. if the html page in the root directory of your project then there must be a typo in the src

Basically I am trying to integrate this package in my angular app. What is the correct method to go about it.
The right way to go about it is to use a bundler like webpack, rollup etc...

Related

Create React App Blank Page when including on existing site

I'm using XAMPP locally to host an exisiting CMS and I want to include my react app within the CMS by using an include (this works fine for including static HTML and other PHP pages). The CMS admin page it'll be included on will look like this: http://localhost/website/administrator/
I need to place my create-react-app in a folder like this though so that the built in CMS script will include it: website\subfolder1\subfolder2\subfolder3\subfolder4\default
Currently I'm getting a blank page where I've tried to include my app. If I remove the embedded I can see the markup is being output, as in an empty div with the root class name.
I've tried setting a "homepage" property in my package.json and various combinations such as '.', and also edited the manifest site_start without luck, i can't seem to get it to render. Can anyone point me in the right direction?
Here are some steps that you could try to get the bundles building correctly, as you will need to override the default behaviour of create-react-app by ejecting.
Make a backup copy of your create-react-app
Eject, so that you will now be able to configure webpack
n
npm run eject
Go to webpack config, see the scripts section
You can change the build scripts as such as start and build
The issue seems to be where the js and css files are built to. You can change where they are output in webpack by editing the config.
See link: https://webpack.js.org/configuration
You could try changing the path for the output section to where your cms html is trying to find the bundle.

How to include javascript third party library in ionic app?

I am sorry to ask this silly question. I am trying to use pathfinding.js (https://github.com/qiao/PathFinding.js/) in ionic for my navigation.html page (a partial file located in the www/templates/ directory) but it's not working with partials files except with index.html root file only. However, it gives me error if I use it in the index.html root file, maybe because it needs to draw a svg in the navigation.html page, but it could not find the navigation.html page since it is in the index.html (dependency with the navigation.html)
I am thinking to use bower, but can bower solve this problem?
In index file add the script tag to add scripts.
<script src="./templates/PathFinding.js/">`
It will get loaded.
However, if you use Bower.. Bower will only download the files to src folder .. then you need to manually add script tags for script into the index.html.

PDF Viewer for Ionic 2: cannot import or require

I am trying to build a PDF viewing component for an Ionic 2 app. I have lots of experience of Angular 2, but not of Ionic.
The component will be built using pdf.js and have created the pdf.js asset to be included in my project as described on the github page. However, trying to require this fails — require is not defined —, so I copied the script to the asset folder and tried importing it. It seems the file is not being copied to the build so that fails too.
Anyone have any pointers for requiring or importing a non-weboack non-SystemJS script into an Ionic 2 app?
Create a js folder (or something like this) in the www folder; include the files needed and reference them from there. This folder is not emptied on build.
EDIT
The root of the www folder does not change and you can reference it as someting like ... 'js/need-this.js'.
There should have some references in your index.html file to the build folder and maybe the assets folder (icon); this concept of using a js folder works the same. Only build and assets will change.
I feel that I should point out that you shouldn't copy anything into www since this is autogenerated and any changes will be overwritten. You should copy into assets instead.

How can I locally store and statically serve the angularjs library?

I am going to be working on an angular project without internet access, so my links to the cdn will not function. I would like to save the angularjs library to a folder inside of my project. Here is what I have tried:
I went to the cdn link, copied all of the text, pasted it into a file, and saved that file as angular.min.js . I then commented out the script tag for the cdn in my index.html file, and put in a new script tag specifying the path to the new saved file. When I open my project, I get the error "Uncaught ReferenceError: angular is not defined."
Here are the relevant pieces of my project's file/folder structure:
project
|_core
| |_public
| |_app
| |_index.html
|_libraries
|_angular.min.js
And here is my script tag:
<script src="./../../../libraries/angular.min.js"></script>
And here is the link to the cdn from which I copied the text:
https://ajax.googleapis.com/ajax/libs/angularjs/1.4.9/angular.min.js
Are you using a local webserver of any kind? Angular and many scripts need to be loaded from http and not file://. For this, you could use python's SimpleHTTPServer: python -m SimpleHTTPServer 8080 -- will open an http server on http://localhost:8080, open the url in your browser and you should be good to go. Also note that the order in which you load the scripts in your HTML is important, and Angular needs to be loaded before all modules of code that use it.
<script src="../../../libraries/angular.min.js"></script>
How about this ?
Have you just tried this?
<script src="/libraries/angular.min.js"></script>
Maybe your server start website (localhost:80) from the root of your project
You can use Grunt task to minify and concatenate all files. You can read the docs of GruntJS. Very useful.
http://gruntjs.com/sample-gruntfile
see the concatenate section.
if you do that manually you have check the sequence of lib files. Angularjs lib need to be on top.
There are chances of errors.
yes just to include a file you don't need Grunt
check in chrome inspect developer tool in network tab if your file angular.min.js loaded properly. If not you can change the path and try but if it is loading and still you are getting same error so please create jsfiddle or plunker of your file so that we can look it in detail.

Can't get gh-pages to work with Angular project

I am working on an Angular project and wanted to host the project through GitHub with gh-pages.
Since the index.html file was not in the root directory, I created an empty branch off of my master and called it gh-pages. I then copied my files into the new branch, so that the index.html file was in the root directory.
I am also using Grunt, so I copied the grunt generated app.js file into the root directory as well. I can now see the project in the browser if I go to http://kelseysteele.github.io/votm3/#/home, however the navigation bar is missing and the other pages, like http://kelseysteele.github.io/votm3/#/overview, are not working properly.
I've been stuck on this for a few days and would really appreciate any assistance with this.
Check your console, you'll see errors: GET http://kelseysteele.github.io/data/overview-data.json 404 (Not Found)
Fix the url here: https://github.com/KelseySteele/votm3/blob/master/src/votm3/Scripts/controllers/overview-controller.js#L10
Because you are using grunt, you will want to build the project before serving the files. This will make the files and code modules build properly before serving the index.html. A popular module you can use is https://github.com/tschaub/grunt-gh-pages an get your builds serving properly.

Categories