Basic steps for setting up a NodeJS application
Table of Contents
Basic steps for setting up a NodeJS application
-
Initial steps
mkdir my-project cd my-project npm init -y
By giving it the -y shorthand flag, you are telling npm that it should take all the defaults. If you leave the flag out, you are in charge to specify the information about your project manually.
mkdir src cd src touch index.js console.log('Hello Project.');
-
Running the code in the index file:
From the project root folder, run this:
node src/index.js
-
Go to package.json and make this modification to scripts.
"scripts": { "start": "node src/index.js", "test": "echo \"Error: no test specified\" && exit 1" }
From now on, we can run
npm start
to run the code in src/index.js or"scripts": { "build": "node src/index.js" }
From now on, we can run
npm run build
to run the code in src/index.js -
Add
.gitignore
file and add this to it:node_modules
-
Using module system:
In package.json, make this modification
"type": "module"
-
Adding additional functionality:
If you add additional functionality using additional files, import them into the index file and run them
import { convert } from './convert.js' console.log('Hello Project.'); convert();