1

I am looking at making a simple hello world with the hapi.js tutorial.

I have installed hapi:

  1. npm init
  2. npm install hapi --save
  3. I get a large set of folders with files

I tried doing node index.js and that gave me errors. So I cd into node_modules and got another error when running node. I cd again into hapi and again got an error when running node index.js. I added all of the syntax from the tutorial.

var Hapi = require('hapi');

var server = new Hapi.Server();
server.connection({ 
    host: 'localhost', 
    port: 8000 
});

// Add the route
server.route({
    method: 'GET',
    path:'/hello', 
    handler: function (request, reply) {
        reply('hello world');
    }
});

// Start the server
server.start();

Not sure where I should be running index.js

2
  • Error message please? Running the program inside node_modules will not gain you anything Commented Mar 31, 2015 at 18:01
  • Ok, that makes sense. I made an app.js file and was able to get everything to work there. However, it was in the node_modules. when I npm install hapi I get multiple files with folders in them. Do I ignore those and go about creating my usual folders for mvc? Commented Apr 1, 2015 at 0:06

2 Answers 2

2

The node_modules folder is used to store all the dependencies for your application (ie. express, hapi, etc). This would be similar to a lib (library) folder in other languages.

When you download dependencies using npm install, the node_modules folder will get placed at the root of your project. Your source files can be placed anywhere within the root or within subfolders you create. They shouldn't however, be placed in the node_modules folder since it is meant for external dependencies.

Contrary to the other answer, you're not restricted to executing the program in the root - you can execute it in any subfolder as well. When you run the program, if Node can't find the node_modules folder in the current directory, it will move up to a parent directory until it finds it. See Node's modules documentation.

Sign up to request clarification or add additional context in comments.

Comments

0

You should ignore the node_modules directory. It is used by npm to install dependencies. You shouldn't make any changes in that directory. Your files should be in the project root:

your-project
  |__ node_modules
  |__ index.html

You need to execute node index.js in the project root, which in this case is your-project.

cd your-project
node index.js

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.