Home > OS >  Why does npm become undefined when I check node?
Why does npm become undefined when I check node?

Time:09-27

screenshot of my terminal

So I'm learning react and I was checking to see if npm had downloaded properly, and it seems to be running fine, but after I type in node and then check npm again, it says that it is not defined. The only way for me to get npm to work is to kill the terminal and make a new one.

Can someone help me understand this? It is not a huge roadblock or anything but it bothers me, because the tutorial I watched to download node would check to see if node was downlaoded and then npm, and did not have this issue.

CodePudding user response:

When you run node in terminal without any keys you start new session of NodeJS application.
It means that every next line you will write would be executed as javascript code until you end the session either by pressing Ctrl C twice, typing .exit or manualy closing the terminal.
In your case you're trying to execute

npm

as a part of the script.
In this case NodeJS environment is looking for any function, any variable or something with the name npm to look into but hasn't find anything.
That's why you're getting this error.

CodePudding user response:

A good way to check if a program is installed and on the path, is using the version / help arguments.

node --version

Different programs have different behaviors when not passing arguments, the ones that require arguments will usually print out the help section. Node will enter interactive mode, where node will run the code written in the terminal session.

TLDR: To check whether a program is installed try

<executable> -v

If your terminal attached to a different program, most programs will let you exit with Ctrl C / Cmd C (Sending SIGTERM signal)

  • Related