Node JS/ Angular - node.js

I have installed Node JS and then imported an existing work project in Visual Studio Code. After that I ran npm install in the project folder, then run npm start and the app comes up fine. However, if I run an ng command I get an error telling me "ng is not valid command", even though the Angular CLI is in the modules folder.
I tried manually installing Angular CLI globally and set system path to point to the npm folder, and then the ng command works fine. What I don't understand is why do I need to install CLI globally if I just want to run that command within the project where the module is already present?

As a general rule then you will need to install globally any commands you wish to use (without NPX). This isn't really a restriction of NPM so much as it is a fundamental way in which command line programs work. The OS will only look in fixed predefined locations set in PATH. This applies to any Node based tool such as grunt or ng or whatever.
(While some systems do look for executables relative to the current working directory, or can be configured to, it's generally not a good or reliable method and NPM doesn't rely on this behaviour).
For something like the Angular CLI then installing it globally should be fine and is what many people will do. As a general rule if it is a command you want to run, rather than a dependency for a project, you can consider installing it globally. You'll notice that on the Angular CLI page the example does exactly that.
In many cases however you might want to run a command from a local project. Perhaps for a build script or something else where you want to keep it isolated. In that case you instead prefix your command with npx which will look inside the local project for commands.

Related

Facing error while installing npm in react

I want to make a react project but when I execute npx create-react-app, it doesn't respond. Can anyone tell me what the issue I am facing here is? Screenshot.
make an empty folder then drag it over VSC, and type in terminal: npx create-react-app . (dot means in the current folder) also make sure u have node installed, type: node -v (to check what version u have)
From the official ReactJS Docs:
You’ll need to have Node >= 14.0.0 and npm >= 5.6 on your machine. To create a project, run:
npx create-react-app my-app
cd my-app
npm start
In the screenshot you provided, you indeed ran the command; but, it doesn’t show that you checked that the directory, visual, was created within your working directory, New folder. In addition, no error message was output; so as it stands, we know the npx command exists; and can only assume that, the command executed without error.
When using create-react-app:
A new directory will be created in the working directory (in your case, New folder) you run the command within.
This new directory will have the name of the argument you provided to create-react-app (in your case, visual).
So your directory structure you look like this:
New folder/
└─ visual/
The issue I see is that, the general output normally seen when running create-react-app (as shown here) did not appear in your screenshot; however, I’ve never ran it from MS PowerShell, as you appear to be. So you’ll want to check:
That you’re not overthinking this, and ensure that the, my-app, folder really wasn’t created;
And that your NodeJS version is either 14.0.0 or higher:
node -v
And that create-react-app was not installed globally:
To check this, run:
npm list -g
If you see create-react-app in the list, run:
npm uninstall -g create-react-app
npm install create-react-app
Or that running it from cmd (or cygwin) instead of powershell is maybe the better option.

Folder structure for Angular and Nodejs

I am going to create an app which deals with Angular5(front-end), Nodejs(middle-end) and MongoDB(back-end).
Below is the folder structure:
Package.json: "start": "ng build & node server.js"
Now when I am start the app by npm start, It throws the following error:
"Unable to find any apps in .angular-cli.json."
I hope node server is looking for this file to load angular codes. But it lies inside angular folder.
If I place both angular and node codes in same place, it works well.
But it looks more clumsy and a bit confusing. I don't want to compromise with my folder structure.
Can anyone please help to achieve the app working on the same folder structure that I would prefer?
Thanks in advance.
it might be that your global #angular/cli installation got corrupt. You may try a cache clean and reinstall..
npm cache clean
npm -g i #angular/cli
If you use angular cli for creating the project, then looks something wrong in the package.json, (as I seen your comment on the previous answer)
first uninstall angular cli if already install by using
npm uninstall -g #angular/cli, then npm install -g #angular/cli after cli installation ng new [project name /folder name]
cd [project name /folder name]
ng serve --open
try once.
I got this same error in my current project and was confused because I'm running the application / ng serve in one terminal, but got this when I tried to generate a component from another terminal. .angular-cli.json was already there and correct. So what gives?
I realized that I used the shortcut to open VisualStudio Code's internal terminal -- which opened the terminal to the *root of the project * (like most IDEs). The project contains other things in addition to the Angular application folder that has the .angular-cli.json file in question. I just had to cd to the right folder and run ng g c again and things were fine.
In my case it was just a silly error. I thought I'd come back to share in order to save people a real headache for something so simple. I see that Shiva actually has mentioned this above, but I thought I would give a bit more detail so it doesn't get overlooked.
Reference URL: How to generate .angular-cli.json file in Angular Cli?

Installation of vue-cli : how does it work?

I'm very new to Node Package Manager and also Vue, and I'm trying to understand what exactly is going on with using the Vue CLI.
The vue.js website has this as instructions for running the official Vue CLI:
I have a few questions about this:
Does npm install --global vue-cli need to be executed only once on a machine, or once on a directory, or once per new project you're starting? In other words, once it's on your computer, is that the last time you need to run that command, or do you need to execute this command every single new project you start?
Once a new project is initiated, are local copies of the newest version of vue (and vue-router, if selected) installed?
If I finish this project and want to deploy it, how do I then port this over to a production server?
Once in a machine, except for the rare cases where one is isolating one's npm install (such as by using nodeenv or inside a container); that's what the global option is for.
After running npm install, yes.
Running npm run build and copying the contents of the resulting dist directory to the production machine (often within a /var/www directory or similar). This can be automated further in many ways.

Cant run custom CLI

I made a CLI for my project. Its in the bin folder:
#!/usr/bin/env node
// myproj/bin/cli.js
console.log('hello');
I linked the bin by using npm link. But when I run
$ cli
I get an error cli not found. What am I doing wrong?
Firstly, npm linking is a two step process as it says in the npm documentation page, so you are missing the second step. Linking is basically a way for you to introduce a dependency in another module you are working on.
Secondly, If I understand correctly what you are trying to achieve (run the module that you are developing locally as a global module), you can just use npm install -g command, from inside the project folder that you are developing.
This will install, your global module on your machine and will allow you to run it.

The directory to use npm to install package

I just get started on nodejs. I have installed nodejs and npm. Now, I want to install some packages like mongodb and express. As my default directory path in cmd is C:\>Users\administrator, do I need to make current folder as nodejs folder to run npm install express/coffee-script or I can just run this command under the default directory path mentioned above?
By the way, I always see the npm install command provided by others starts with a dollar sign, but I can only use the command without the dollar sign. So what does the dollar sign stand for?
By default, npm will run in local mode, and install scripts into ./node_modules. This is great if you need to require your scripts, as you'll do with Express.
Calling it with the -g option installs it globally, wherever node is installed (usually, on Linux, in /usr/local. This is great for packages that are meant to be run using the shell (for example, Supervisor).
Generally, if you want to develop a node.js application under C:\foo\bar\myapp, you will run npm from there.
FYI, the $ sign is a general indication meaning that the following command is meant to be run on the command line.

Resources