Is it ok to have a node.js project with no package.json? The ones I see on the internet all come with package.json
What is the effect of having no package.json?
It is created automatically if you write npm init
.
Then, every package you add using npm install packagename --save
will be added to the dependencies list.
You need package.json
so that when you want to use your project on another machine you don't have to copy all node_modules
, but only your .js
files you have written, assets and package.json
. You can then run npm install
command and it will automatically download and install all the required modules (found in the list of dependencies inside package.json
).
You can also manually create or edit it, but it's easier to add --save
when installing a module so you don't have to worry about package versions and stuff like that.
Also if you want to create a npm package, an open source project or stuff other people will use, it's either required or the norm to have this package.json
file describing your project.
package.json is npm file, if you don't use npm you will not have this file, npm is a great tool if you want to use external libraries in your project but if you don't need it (which is very not likely unless you are doing something very simple), you don't need package.json file too.
To generate package.json file initialize npm in your project using npm init
Fundamentally, package.json
is a meta file for your application. It lists all the configuration of your application.
What is the effect of having no package.json?
Nothing as far as you're running all your code locally and have no requirement for deployment whatsoever.
Let's setup a scene for you to understand this better.
Imagine that you wrote a brilliant application using node. Now all the chicks in your surrounding want it to play with. It is so fantastic!
Now you want to give it to them and during the development process you npm install
ed so many things that your project grows beyond 4TB size.
There is no data storage device available to shit that huge code base.
Then the girl of your dream said I want it and I want it now. So you begin searching for app deployment process for node applications.
That is where you stumble upon a magical thing called package.json
.
So what you do is you list all your npm install
ed modules under dependencies
property. Then you delete node_modules
folder, add package.json
and commit the entire damn thing in github. Even the .zip
file is of 10MB
Then she gets the code.
Types in npm install && npm start
(which will install all the dependencies from the package.json` and start your application)
If you have package.json however, that is where you specify all your dependencies.
Using --save
flag of npm install
Example.
npm install express --save
How is package.json created in the first place? Is it created automatically?
You can manually create a text file and save it as package.json
OR
A more sophisticated way is to use the command
npm init
I am wondering why I do not have package.json
Me too! :)
You're most probably following a tutorial that doesn't emphasize on initial configuration of the project OR the author of those tutorials presume that the reader has all the fundamentals down to begin with.