Andy J Andy J - 1 month ago 8
Node.js Question

Can't execute page with "main" but working with explicit path

I'm a nooob at Node so it's likely due to my ignorance that I'm doing something else than what I think I am. Due to the same reason, I haven't seen anything helpful when I googled the issue.

When I'm running my page like this


node app/index.js


it works and displays the right text in the browser as I head to localhost:3001. Then I tried to specify in the JSON file the path like this


"start": "app/index.js", ...


and executing node without any arguments. My understanding was that since there's no arguments given, the program till look up something in the configuration file.

I noticed that when I specify the path explicitly, I get nothing in the console and it "hangs". But when I go with node alone, there's a bracket like if it's waiting for something. No idea what, though...

For some reason it doesn't and the page can't be shown. What can I do about it?

Answer

start command in your package.json file should looks something like this - "start": "node app/index.js". And then to run it you need to execute npm start. node command without parameters just stars node-repl. Read more about npm and package.json at npmjs.

The main parameter in your package.json is used as the entry point to your package. To run different scripts you should use node [path_to_script].

The main field is a module ID that is the primary entry point to your program. That is, if your package is named foo, and a user installs it, and then does require("foo"), then your main module's exports object will be returned.