Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start main index.js from my package.json file

Tags:

node.js

npm

I have installed node.js and I have created with npm init -f my very first module.

In created package.json file I see "main": "index.js", which should be starting point of my application. So in same folder I have created this file and I have putted there console.log('test')

The problem is that I don't know how to run it without declaring "scripts": { "start": "node index.js" }

Should I always have scripts section. Because I'm missing why there is entry point of application when I have to use scripts section anyway.

Thanks

like image 547
Keri Marr Avatar asked Dec 21 '18 20:12

Keri Marr


People also ask

How do I start an index file in javascript?

To run this file, you should open up your terminal again and navigate to the directory in which you placed index. js. Once you successfully navigated yourself to the right spot, run your file using the node index. js command.

How define main in package json?

main. 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. This should be a module relative to the root of your package folder.

How do you run a custom script from a package json?

To execute your Script, use the 'npm run <NAME-OF-YOUR-SCRIPT>' command.


1 Answers

Please do read the Getting Started Guide | Node.js. You can start the web server by typing

node index.js

if you have the index.js file specified in package.json as follows

{
    ....
    "scripts": {
        "start": "node index.js"
    }
    ....
}

You can also run the server by the following command

npm start

You can read about the difference between npm start and node index.js here

like image 101
Javapocalypse Avatar answered Oct 20 '22 19:10

Javapocalypse