*This article has been republished from *medium.com
For beginners, when you start to learn Node.JS, it’s very common to do all like these :
create folder, eg: mkdir hello-world-one
*cd *hello-world-one
type npm init and answer some questions
after finished npm init
with an editor, we create helloone.js file like what you typed when answering *npm init *command
edit helloone.js
and run with node*** helloone.js***
open browser, and check url localhost:port-number, eg : localhost:3000
and then, if want to create more Node.JS app, you do those steps again, one more time,
create folder, eg: mkdir hello-world-two
*cd *hello-world-two
type npm init and answer some questions
after finished npm init
with an editor, we create hellotwo.js file like what you typed when answering *npm init *command
edit hellotwo.js
and run with *node *hellotwo.js
open browser, and check url localhost:port-number, eg : localhost:3000
On those two steps, when you create a very basic Node.JS application, you could possibly installed the same npm modules multiple times, like :
npm install express** **on hello-world-one folder and
npm install express** **on hello-world-two folder.
on limited and expensive SSD drive, node_modules folder could be a victim for disk space waste,
Now we create a folder named *hello-worlds, *eg : *mkdir *hello-worlds
*cd *hello-worlds
type npm init and answer some questions
after finished *npm init *your package.json could be looks like :
{> “name”: “helloworlds”,> “version”: “1.0.0”,> “description”: “”,> “main”: “helloworlds.js”,> “scripts”: {> “test”: “echo \”Error: no test specified\” && exit 1"> },> “author”: “Dendy B. Sulistyo”,> “license”: “ISC”,> “dependencies”: {> “express”: “ ^ 4.16.4”> }> }
create helloone.js and hellotwo.js
run npm install express
so… now you only have one folder with two js files, before you run
node helloone.js
dont forget to edit your package.json file first, change helloworlds.js to **helloone.js **and vice versa for hellotwo.js file.
before run node hellotwo.js , change **package.json **file, replace helloworlds.jswith **hellotwo.js **, save and run
node hellotwo.js
now check your disk usage when you use this way.
on a big project and more npm modules, you will save a lot of disk space.
by the way, when you create small project, package.json files is not so important, but when your project grow up, and you need more npm modules, the main line and the dependencies line are important to focus related to the needs of npm modules ( node_modules )
lets try and save some space.
#node-js #javascript