On Jul 23, 2016, at 12:53 PM, Sorcerer Stone wrote:
>
> Newbie in node.js. Reading a book and download projects to follow the text. I understand the primary role of index.js file is the entry point for a given project. But I have problems with this file.
Welcome to node!
There's nothing in node that says your entrypoint has to be called index.js. You can call it anything you like. Sufficiently large projects might even have multiple entrypoints. It's entirely up to you as the developer of your project what you name your files. If I were writing a typical node project that will be a web server, I would likely call my entrypoint file server.js. I would then start my server by running "node server.js", or I might specify that as a start command in my project's package.json file.
> The first project in the book is to create a new directory (TestProject) and download and install a project using the command "npm install colors" in this directory. The book went on and show how to modify the index.js file.
>
> After I ran the above command, a node_module folder is created in the root directory (TestProject) and no other files beside this node_module exist in the root directory. I attached screenshots for my questions.
It is normal that when you run "npm install somemodule", a directory "node_modules" is created in the current directory (if it does not already exist), and the module "somemodule" (and its dependencies, if any) are installed into that node_modules directory, and nothing outside of that directory is modified. There should normally be only one node_modules directory in your project, and it should be at the root directory of your project, so you should be in the root directory of your project whenever you run an "npm install" command. You should not need to open the node_modules directory or concern yourself with its contents. It's just a place for npm to install the modules you asked npm to install. You then write your own JavaScript files (outside of the node_modules directory) which make use of those modules as you see fit.
Checking the documentation of the colors module...
https://www.npmjs.com/package/colors
...I see that you would use it by writing (in a file that you create, named whatever you like):
var colors = require('colors');
Then you can use that "colors" variable to access the functionality of the colors module.