Cannot use ng add and npm install doesn't meet the requirements which are somewhat unclear.
How do I add an node module to angular app/project? I've installed it with npm install so what's next?
I tried adding it into scripts array in angular.json but is caused just error "require is not defined"
Cannot use ng add and npm install doesn't meet the requirements which are somewhat unclear.
How do I add an node module to angular app/project? I've installed it with npm install so what's next?
I tried adding it into scripts array in angular.json but is caused just error "require is not defined"
Share Improve this question asked Dec 17, 2018 at 13:58 user1271930user1271930 3411 gold badge8 silver badges22 bronze badges 3-
adding the dependencies to package.json and running
npm install
does that not work for you? – nircraft Commented Dec 17, 2018 at 14:03 - @nkuma_12 Isn't npm install for adding dependencies - no need to add dependency but just to install one with npm. Screenshot of what? – user1271930 Commented Dec 17, 2018 at 14:17
-
Well, in that case you can download the
tar
or.gzp
of the npm library you need, and keep it somewhere in the project directory and refer to it in index.html for use. – nircraft Commented Dec 17, 2018 at 14:21
3 Answers
Reset to default 1If I understand correctly, the answer to "what's next" is this:
In your angular.json
file, add the scripts to the scripts tag:
"scripts": [
"../node_modules/itsFolder/dist/js/its.js",
// Add any other scripts here
],
This includes the script as part of the build process.
Also, check out the answers here: Use external javaScript library in angular 4 application
Example:
import * as md5 from 'js-md5'
ngOnInit(){
console.log(md5(""));
}
Soruce: https://www.freecodecamp/forum/t/can-i-use-npm-packages-directly-inside-angular-2/132935/6 NOTE: I have tested this and it works. You can include any npm package and try in Angular 'ts' file, knowing what you are doing with it and how to do it. Thank You!
Below steps might help you to start your first project in Angular. Nicely explained here
Go to https://nodejs/en/download/ and select npm version based on your OS (Windows or Mac)
To check npm is installed, run npm -v
. it will give you installed npm version.
Another mands to install latest npm
npm install -g @angular/cli
To check available CLI mands type this.
Now you can use below mands to Create New Angular 4 Project
ng new first-project
It will create Angular project first-app and will install required npm packages.
To start project, you can use
ng serve
To create Angular 4 Component
ng g ponent cities
How to create Router for The Newly Component
To make access the new ponent, we have to make routing for it. Open and edit src/app/app.module.ts the add this import.
import { RouterModule } from '@angular/router';
imports: [ BrowserModule, FormsModule, HttpModule, RouterModule.forRoot([ { path: '', redirectTo: 'newComponentName'},
{ path: 'ponentname', ponent: ComponentName } ])
]
Next, replace all tags in src/app/app.ponent.html with this tag.
<router-outlet></router-outlet>