Where to place JS files for NodeJS to see them

I’ve just installed NodeJS on my Mac, and i got it working in the terminal, using inline scripting like “console.log(‘Hello world’); works fine.

But where do i place JS files for NodeJS to find them? Can i specify the root folder NodeJS to look for file in?

I followed this guide: http://nodeguide.com/beginner.html#learning-javascript
but i cannot get any of the samlpe to work where i reference a script file.

You put them in whatever folder you want. It is common practice to put each application in a different folder.

Then you run node.js like this:

node /path/to/file.js

Or like this:

cd /path/to/
node file.js

Where file.js might look something like this:

console.log('hello world');

You’ll have to navigate to the correct folder “manually”, in the Node Command Line Interface (CLI).

If you need to change drives, type the drive letter and a colon to switch to that drive, like so;

C:> (<- this is the line prompt, yeah? Just add this after it -> D:

That changes the drive. Now write cd (CD = “Change Directory”) and the name of the direcotry you want to go to the directory your stuff is in:

D:> (<- the new prompt. Write something like this after it: ->) cd myprosject\subfoldername
D:\myproject\subfoldername> (<- your new line prompt – if “myproject\subfoldername” exists)

now ask node to execute your script (that is stored in myproject\subfoldername, like so;

D:\myproject\subfoldername> node helloworld.js

Remember to write “node” first – otherwise the command won’t go to node, but to the OS, which will probably just open up the js file in a text editior instead of running the goodies inside.

Read More:   Falsey values in JavaScript

It is very easy.. Go to your command line. navigate to the file location..
then simply run the node helloworld.

I’m not sure I understand. it doesnt ‘look’ anywhere for your .js files you point at them when you run node. Like so, on the command line:

node mynodeapp.js

If you’re meaning where does it look for your .js files as modules, when requirign them, like so:

var mymodule = require("mymodule");

Then it will look inside a folder names node_modules. But I’m sure you’re looking for my first example above.

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts