Newbie question about the location of index.js

1,635 views
Skip to first unread message

Sorcerer Stone

unread,
Jul 23, 2016, 1:58:47 PM7/23/16
to nodejs
Hello all,

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.
 
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.

My question is where is index.js located? Should it be in the root directory (TestProject) of this project or at least in colors's folder where package.json is located (Path of Index.js 01.jpg)? Instead, I found an index.js file buried in the subdirectory, the lib folder, within the colors folder after the project was created (Path of Index.js 02.jpg). Is this normal?

In more complicated project, there are lots of index.js files created for each dependent modules buried under node_module folder. For this simple module, I would expect the location of index.js should be in the root directory and not buried deep inside node_module's or colors' folder. But this root directory (TestProject) is empty. Not sure what is the problem or is this a problem at all.

I am using Windows8, version of node.js is 4.4.7 (LTS version) and npm is 2.15.8.

Could I be running into this bug:
https://github.com/npm/npm/issues/5082

The above post said npm 2.15.8 should fix the missing index.js problem.

Thanks in advance.
Sorcerer
Path of Index.js 01.jpg
Path of Index.js 02.jpg

Ryan Schmidt

unread,
Jul 26, 2016, 9:38:16 AM7/26/16
to nod...@googlegroups.com

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.

pspi

unread,
Jul 26, 2016, 9:38:16 AM7/26/16
to nodejs
Hi Sorcerer,

Did the tutorial ever instruct to create a base project structure before running the 'npm install' command? Such structure might include package.json and index.js files in the project directory. Then modifying that index.js would make sense.

The 'npm install' command is used for installing dependencies to an existing project (one that already has a package.json and source code). Npm installs the dependencies under 'node_modules' directory and that is exactly what you've found by investigating the colors package interals (some packages use index.js as entry point and some configure their own in package.json). However It is unorthodox to modify any files under 'node_modules' and causes your version of modules to be incompatible with what other developers expect them to be.
Reply all
Reply to author
Forward
0 new messages