How do I get globally installed node modules to run on terminal - linux

I am learning how to code with react, and I have installed the create-react-app on my computer. However, globally installed node modules are not running on terminal.
I installed create-react-app globally. I can find find the folder, but I don't know how to get the command to run. Please how do I get globally installed node modules to run on terminal. I used yarn for the installation.

for create-react-app, you should be able to simply run
create-react-app then the name of the App you are creating
so:
create-react-app nameOfApp
Here is some documentation that should help:
https://reactjs.org/docs/add-react-to-a-new-app.html

If you are asking how to start the application, you can use
node server.js
if your entrypoint file is called server.
Alternatively use npm start, documented here: https://docs.npmjs.com/cli/start
To see what is installed,
npm -g list
will tell you what is installed globally.

I finally resolved the issue and this is how I did it:
I found the location where my global packages where being installed and exported the path to the .bin directory
export PATH=$PATH:~/.config/yarn/global/node_modules/.bin

Related

'npm install -g typescript' doesn't install correctly on windows (missing tsc.cmd)

Global install of typescript doesn't appear to install correctly on windows.
Steps to reproduce:
.Using node v16.16.0 (64bit)
.Then 'npm install -g typescript'
.I can confirm environment path variables contain:
'C:\Users{MyUserName}\AppData\Roaming\npm' and
'C:\Program Files\nodejs\node_modules'
The node_modules/typescript/bin folder appears a follows:
As seen, a tsc file does exist, however the expected tsc.cmd doesn't.
This is required for windows to execute.
My assumption is this must be system or permissions problem whereby node can't create the tsc.cmd, not necessarily and issue with the
typescript package
Image for reference:
//----------------
Additional notes:
I have also tested this on 10.24.1 using nvm, this is now uninstalled
I have ran these steps on another machine, without problems countless
times.
I have uninstalled (completely) nvm and node before attempting this
multiple times.
I have performed a complete system search for tsc.cmd I do have
wsl installed, perhaps there is some interference there?
Results from suggestions this far:
Running locally on a project (Niceman)
I think this is an issue more related to your node path in your environment variables. To test this, install something else globally, like sass for example.
Then you can try sass --version. If it doesn't work the it's probably that your node path is messed up. You can try uninstalling node and reinstall it with the last LTS installing file from the official site. Then you can try to installing typescript again. If that doesn't work you'll need to open the environment variables and check your path for node modules.
Another possible solution is to try using another command line, because maybe a Powershell config is messing up this things. You can try installing git bash. It's not the best, but works.
Solution: use yarn!
As mentioned, I am missing tsc.cmd whatever method I choose (win 11). Who knows what the cause is.
By using yarn :
Delete the dudd folder under your node_modules folder from any npm attempts
yarn global remove typescript
yarn global add typescript
add 'C:\Users{yourUserName}\AppData\Local\Yarn\Data\global\node_modules.bin' to your path
As mentiond, you now have the required tsc.cmd:
What a ride ey!

How does Node.js know that I have a certain package installed?

I've mostly been using node in a text editor. But I was playing around with Node in the terminal today, and I typed: let Web3 = require('web3'); which is a package. I was not expecting this to work, but it did. How does node know that I have this package...did I install it globally before?
And what "environment" am I in when I run node on the terminal? Is it my current working directory? (I'm asking because when I run npm install [package] I know that that package is installed locally in my project directory, but not elsewhere)
You can install packages globally(system wide). These are available system wide. So you may have installed web3 globally.
To install globally you need to use -g flag like this
npm install -g web3
And what "environment" am I in when I run node on the terminal? Is it my current working directory?
Yes node starts where you run node. Module resolution depends on the start location as well.
Module resolution algorithm in detail:
https://nodejs.org/dist/latest-v15.x/docs/api/modules.html#modules_all_together
Directories with a package.json files treated by npm as a project folder. So, that means three things:
Your environment is not a working directory, but the one with a package.json file in it.
Calling npm install [package] will install this package in a node_modules folder on the same level as package.json file.
Node has a module resolution system which will look for required package in closest node_modules folder and some other places.
If you are interested in how it works in more details here is a nice article

NPM on OSX, error getting global installed packages

I have nodejs installed on a MBP which runs OSX 10.9, I have installed as a package downloaded from the nodejs website. Then I have installed the MEAN stack following instructions on mean.io.
The commands are:
sudo npm install -g mean-cli
mean init yourNewApp
That works correctly
Now the real issue is when after my app is created I enter the dir using the terminal, and write gulp, and it thows me some errors that some mandatory modules are not found.
The modules are written in the package.json file that mean generated, and they are installed as global modules on ~/.npm
I browsed the folder and there are all the required packages folders, inside the folders there is a package.tgz file which has the code of the package and a package folder which holds a package.json file describing the package itself.
Now I don't understand why the packages are compressed and why if they are installed globaly can not be accessed from gulp on my project folder.
Thanks in advance.
If you install some global module then you better don't put it into the package.json of your app because when you run your app that's the first place where is going to search and if it is there your app is going to look at node_modules folder and if it is not there your app will crash.
My advice is try to install your modules inside your app, npm install your_module --save because your app is gonna be portable and with a simple npm install you will be able to install all your needed packages.
But if you still wanna install global packages you maybe wanna follow this rules:
If you’re installing something that you want to use in your program, using require('whatever'), then install it locally, at the root of your project.
If you’re installing something that you want to use in your shell, on the command line or something, install it globally, so that its binaries end up in your PATH environment variable.
If you have the time to read the link then you will see that there are exceptions and how to handle them.

Locally installed versus globally installed NPM modules

In my package.json file, I have bower listed as a dependency. After I run npm install, bower gets installed locally. When I try to run bower after installing it locally I get an error
"bower" is not recognized as an internal or external command
It seems the only way to resolve this is to install bower globally. Why should I have to do this? If my project contains a local copy of bower, why won't node use it?
Installing locally makes bower available to the current project (where it stores all of the node modules in node_modules). This is usually only good for using a module like so var module = require('module'); It will not be available as a command that the shell can resolve until you install it globally npm install -g module where npm will install it in a place where your path variable will resolve this command.
Edit: This documentation explains it pretty thorougly.
You can execute your local instance by typing the line below in cmd:
node_modules/bower/bin/bower <bower args>
We use both PHP and JavaScript, so we have composer and npm.
Each of the projects we work on have different packages both for runtime of the package as well as build/dev tools.
As there are version constraints in each project, installing version x of a package globally (that would be run from the command line), would cause us issues, we install all the tooling in each package. Much easier to define in the appropriate composer.json / package.json files.
But running the CLI tools is a pain if you have to constantly add an additional path to the command.
To that end, we have recommend to the team that the following paths are added to your $PATH in the appropriate .bashrc (or equivalent):
./vendor/bin:./node_modules/.bin
(EDIT: For Windows, the paths would be .\vendor\bin;.\node_modules\.bin;)
So, whilst in project X, we have access to the CLI tools for that project. Switch to project Y, and we get that projects tools.
Sure, you are going to get duplications, but each project is maintained by different teams (and some people are in multiple teams), so again, having 1 version in the global setup is an issue there.
Usually you install NPM modules globally if you want them included in your path to be ran from the command line. Since it is installed locally you will have to run it from the node_modules folder.

Can't you install socket.io as a global package?

I tried to install socket.io with the -g switch
npm install -g socket.io
and it installed correctly I think.
but running the app it throws the cannot find module error.
Local install, i.e. if socket.io is present in node_modules in my project/package, works though.
So can't it be installed globally?
You misunderstood the meaning of global installation. It allows you to access packages directly from your console. But if you want to require package into your own application, you should add it as a dependency into your packaje.json and install it locally.
Here is the quotation from npm documentation:
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line.
When you install a package globally, what you're saying is that you want to use it in your shell (cmd.exe). Something like nodemon is such a package that you would install globally but not include locally as it doesn't need to be included for your app to run, but instead is used to benefit you as the developer.
You install packages without the -g switch when it is necessary for your app to run. In this case it is necessary for socket.io to be installed. Note that locally installed packages should go into your packages.json file so that anyone who installs your app at a later date automatically gets the included packages. Global packages do not appear here.

Resources