New to nodejs - moving around?

Hi all,
My summer project has been to get a grip on node.js
so far its been good and Im quite impressed with the power that the language has however i seem to be at a snag…
Ive got my project folder, lets call it “project” and within that I would like to run the file server.js - the folder is placed on the desktop in Linux mint

Ive read a little and seen that you are meant to cd to the location similar to the windows command line however when i try this I get an error. As this is my first time using the node command prompt in linux im still not quite with all of the commands.

when you made your project, did you run npm init -y from within the project directory?

This is needed to properly run stuff; like with nodemon.


# installs globally first (needed for linux)
sudo npm i -g nodemon -y
# then within the project
npm i --save-dev nodemon -y

Then you run your server.js file:
need to be within project directory!

nodemon server
1 Like

It seems you ran node first and then tried to cd into the project directory, which is why you get cd is not defined, because of course it’s not defined within node.

You need to cd to the project directory first and then run the server with node server.

If you’re using utilities like nodemon with them respectively.

2 Likes

https://www.hacksparrow.com/node-js-how-to-exit-from-the-node-console.html

Perfectly understandable mistake!

1 Like

that was exactly what I was doing, however when I open the folder in terminal and then run node it displays server undefined. That should work shouldn’t it?

Capture

I’m currently installing nodemon to see if the other suggestion from Dynamic_Gravity works.

You just need to enter node server, not node and server in the node shell.

1 Like

I’ll try that when I get home and see how that goes

You are getting a bit confused here at the differenes between a shell and a REPL. It’s important to note here the distinction between a shell and the node REPL which you typed into the terminal.

The shell in your illustrations is the terminal in which you run programs. The shell part is the ~/Desktop/ssh_server line and usually ends in a $ or a #. It’s the part where you typed node into and has commands like cd to change into and manipulate directories.

As soon as you typed node into the shell, it gave you a > symbol which indicates that you’re in the node executable and it’s important to understand that the REPL is for executing JavaScript snippets only, which means that commands like server, or cd have no meaning in this mode.

Node.JS works on modules, where each module is its own file. There are two common ways you can load a module within nodejs: The commandline and via a require function call in the REPL:

$ node ./server

in your shell, or

> require('./server')

Note you can omit the .js file extensions from your module paths

in the REPL. Both will acheive the same thing from the shell and the REPL respectively. NodeJS simply requires the specified module from the first argument passed in from the shell for you without having to type it manually.

1 Like

thanks for the explanation @tjw ! helps to understand a lot. got the server started, now just figuring out the errors in my code. Thanks to everyone that has posted.

1 Like

You’re welcome. I do node.js as my daily job, so if you have a question feel free to make a thread, there are almost always resources around here to help you with most things.