New to Node. How do I use package.json files and what exactly is it for??

225 views
Skip to first unread message

Frank Z

unread,
Mar 26, 2014, 10:49:42 AM3/26/14
to nod...@googlegroups.com
I bought a book on Node.js since I'm teaching myself backend development, and in this book the author talks about the importance of package.json files. He emphasizes that it's great for keeping track of module dependencies in a json format, and installing them all at once using "npm install" in a terminal. So far so good...

But how do I assign my package.json to a particular node.js file? Just stick it in the same folder??? Does it not clash with the package.json file that comes with locally installed npm modules? Do I just make the Name key in the json file the exact same as my js file?

Maybe the package.json is simply for putting your project on npm?

I'm totally confused....

Would be cool if there was a node.js expert I could chat with over skype. I'm so lost.

Angel Java Lopez

unread,
Mar 26, 2014, 11:04:42 AM3/26/14
to nod...@googlegroups.com
Partial and short story:

Your project/application resides in a folder.

In that folder, package.json describes your application. Some important things:
- name
- dependencies
- scripts
- devDependencies

When you or someone else runs:

npm install

from SUCH FOLDER (usually the root folder of your project), the declared dependencies are installed (in node_modules subfolder; the package.json of those dependencies then don't clashes with this package.json)

If you specify a script named test in package.json, then

npm test

runs that script

If your application is an NPM package to be published, the version field is important. You can publish your package using

npm publish

having an NPM personal account.

There are more uses and variant, but these are the first ones to grasp

For example, this package.json describe one of my examples:

(it uses a package.json for many subfolders)
(notice private: true)

Another one, describing an express sample:

And this one describe one of my NPM modules

Angel "Java" Lopez
@ajlopez




--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt

unread,
Mar 26, 2014, 11:05:41 AM3/26/14
to nod...@googlegroups.com
Starting simple, package.json is for defining your project's dependencies. It does lots of other things, but this is the key thing.

To start a project, create a folder, and run "npm init" in that folder.

Then when installing npm modules use: npm install <name> --save

This adds them as dependencies to your package.json.

There's more to know, but that should get you started.


--

Aria Stewart

unread,
Mar 26, 2014, 11:05:25 AM3/26/14
to nod...@googlegroups.com
On Wed, Mar 26, 2014 at 07:49:42AM -0700, Frank Z wrote:
> But how do I assign my package.json to a particular node.js file? Just
> stick it in the same folder??? Does it not clash with the package.json file
> that comes with locally installed npm modules? Do I just make the Name key
> in the json file the exact same as my js file?

In general, a node.js project will not be a single file -- it'll be a whole
folder, probably looking something like this:

index.js # your main script entry point. Name can vary.
package.json # information about your package. Yes, everything's a package.
node_modules/ # this is where the modules you install with "npm install" go
yourfirstdependency/
package.json # You get the idea that it's packages all the way down.
firstdep.js # Its entry point may be named differently.
>
> Maybe the package.json is simply for putting your project on npm?

While it certainly makes that possible, it's actually more core to your
project. Think of it as a roadmap or introduction to your project.

"Hi! I'm _______, version _____. I do this neat thing. To do that, I use _____
and ____ and ____. To test me, run ______ and also, if you want to start me as
a service, run ________"

A good portion of that's optional, but that's the idea.

It's really okay to make a new folder for every project. If you're used to
working in a heap of unrelated stuff, that feels weird at first -- it sure was
strange to me coming from using a ton of tiny ruby scripts -- but I think it's
refreshing once you get used to it.

Aria

Ben Taber

unread,
Mar 26, 2014, 3:44:37 PM3/26/14
to nod...@googlegroups.com
node's docs on how modules are loaded, and how package.json files are looked up

npm's docs on the fields in package.json
https://www.npmjs.org/doc/json.html

Read through both and you should have a pretty good understanding.

Frank Z

unread,
Mar 26, 2014, 5:21:21 PM3/26/14
to nod...@googlegroups.com
Everyone has been helpful except Ben. Just pointing me to documentation...Shame. I've been to those sites and their explanations suck, that's why I'm here!

Otherwise, thank you Angel Java Lopez, Matt, and Aria Stewart for trying your best to answer my questions!


--
--
Job Board: http://jobs.nodejs.org/
Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
You received this message because you are subscribed to the Google
Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com
To unsubscribe from this group, send email to
nodejs+un...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/nodejs?hl=en?hl=en

---
You received this message because you are subscribed to a topic in the Google Groups "nodejs" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/nodejs/RK3EL4f2GUM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to nodejs+un...@googlegroups.com.

Ben Taber

unread,
Mar 26, 2014, 10:05:19 PM3/26/14
to nod...@googlegroups.com
Wow, a simple "Thank you, but I've read those docs and I'm still confused about x, y, and z" would have sufficed. Instead you've insulted me for trying to help, and the people who authored or contributed to those docs that "suck".

Personally, I found the combination of those two pieces of documentation, plus a bit of experimentation, to be very helpful in grasping the concepts.

Good luck figuring it out.

Ben

Frank Z

unread,
Mar 26, 2014, 11:07:55 PM3/26/14
to nod...@googlegroups.com
But Ben, you didn't try to help me. It would be an unearned "Thank You" if I said Thanks. I cleared was asking for specific information on a specific subject, and like many people on websites like stack overflow, you just send me over to the documentation; which is ridiculous frankly. If you asked someone how to put a lid on a blender and they tossed you a 50 page manual, you'd be justified to tell them to go to F*cking Hell.

These other kind people took the time to try to address my specific problem in their own words, you didn't. It wouldn't be fair to THEM for me to thank your laziness.

greelgorke

unread,
Mar 27, 2014, 4:42:54 AM3/27/14
to nod...@googlegroups.com
omg...

Frank, your original post does not indicate that you read the docs, only a book. the natural way of education about a new library is to read docs first.
second, it is always the best way to seed a hint and point to the official documentation together. so you don't have to relie on more or less subjective opinions, but to use them to improve your own understanding of the docs.

In respect of that all your helpers including Ben did a great job: personal explanation in a varariety plus examples plus the link to official documentation.

you should be thankful to all of them. I'm sure, in abscence of explanational posts and examples, Ben would have contributed more to your education. And i'm sure there are more ppl who read this thread and didn't post anything, because all has been said already.

In general, if someone tells you to read the docs, want you to contribute youreself to a better understanding, beyond a specific aspect. besides there is a common sense about how to ask questions, and honestly, you did this one not very well.

at last: a good tone is the grain of a community. more of such reactions, and be sure, soon noone will want to support beginners anymore, because noone like to be insulted even less by a rookie.

Great job anyone, never thought about package,json in so many different ways :) for me it was always a package descriptor, pretty much like pom.xml in maven or gemspec in ruby. which seems so common to me, that i didn't though someone would have real struggles to understand it.

Greets

greelgorke

Floby

unread,
Mar 27, 2014, 5:32:22 AM3/27/14
to nod...@googlegroups.com
Also, if I may add something, your question sucks.

It clearly shows that you didn't even try to create a folder and a package.json file to see what `npm install` does.
People here are not your personal army of minions fetching knowledge for you. If you want to be respected and/or considered, you should follow simple rules that have always applied on tech lists.

  • Clearly state what your problem is
  • Explain what you tried and how it failed
  • Some useful information about the context of your problem
Your original question sounded to me like "I read the first chapter of a book and now I'm asking around because reading the whole book is too long".

If I had seen your post first, you'd probably have got a LetMeGoogleThatForYou link from me. In this regard, even Ben's answer was nicer.

Also, trying to point out that people in a mailing list do not following your understanding of the netiquette is a big failure most of the time. I'm doing it right now because your behaviour leaves no doubt.

Please try and do things before asking.

Alex Kocharin

unread,
Mar 27, 2014, 5:44:34 PM3/27/14
to nod...@googlegroups.com
 
> These other kind people took the time to try to address my specific problem in their own words
 
Do you really think people should try to write these things over and over again each time this question is asked, instead of pointing out to a priori correct and officially maintained documentation? eww
 
 
27.03.2014, 07:07, "Frank Z" <fran...@gmail.com>:
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.

Frank Z

unread,
Mar 27, 2014, 5:55:20 PM3/27/14
to nod...@googlegroups.com
Maybe if they pointed to a specific point or page in the documentation, then maybe a link to the documentation is could be fine. Otherwise I just wanted a concise answer to a specific question. Again, it's like asking someone how to put a lid on a blender and being handed a 50 page manual. The documentation is a lot to read...



Thank about it! ;)

Karl Yang

unread,
Mar 27, 2014, 11:21:55 PM3/27/14
to nod...@googlegroups.com
Why dose the official documentation exist? It's for the beginner to have a good global view and it's the best explanation. If you want to learn a new technology, the best way is to read the official documentation and then buy a book to learn how to start your application. But you bought a book, opened any page, saw the words 'package.json', then came here asking a suck question without reading your book or documentation.
Many people contribute to the code, documentation and this communication, they make node.js easy and free to use. But what make you feel their work or answer suck? No one need to take care you like baby. Please be grateful.

Frank Z

unread,
Mar 27, 2014, 11:28:19 PM3/27/14
to nod...@googlegroups.com
Hey Karl, when was the last time you got laid?

Conrad Pankoff

unread,
Mar 27, 2014, 11:32:35 PM3/27/14
to nod...@googlegroups.com
Seriously? You realise you're just digging yourself deeper into the hole of "looking like a complete fucking idiot," right?

po...@pomke.com

unread,
Mar 27, 2014, 11:39:20 PM3/27/14
to nod...@googlegroups.com
Wow..

Frank, I'm not sure node.js is really the language for you. This certainly isn't the kind of community that behaves the way you are right now. I think the appropriate and mature response from you at this point is 'yeh.. sorry for being an ass'.

- Pomke

Frank Z

unread,
Mar 27, 2014, 11:44:32 PM3/27/14
to nod...@googlegroups.com
Not really...

I've been on stackoverflow.com for a while and that place is notorious for people just answering people's coding questions with nothing more than a link to the first page of some API or Language's documentation.


Adam Brady

unread,
Mar 27, 2014, 11:44:45 PM3/27/14
to nod...@googlegroups.com
+1

Frank Z

unread,
Mar 27, 2014, 11:48:21 PM3/27/14
to nod...@googlegroups.com
Just looking at Node.js' documentation feels like looking at a dictionary. If I used this as my sole guide for making a node.js app, I'd probably never even think to use a package.json file, ever... (and what's the point if I'm not submitting my app to NPM?).


po...@pomke.com

unread,
Mar 27, 2014, 11:51:06 PM3/27/14
to nod...@googlegroups.com
And yet Ben did not link to the first page of the documentation, he linked to to the section about modules (what you were asking about) and a very specific section about the layout of package.json files? Did you even follow Ben's links?

Aria Stewart

unread,
Mar 27, 2014, 11:50:28 PM3/27/14
to nod...@googlegroups.com
On Thu, Mar 27, 2014 at 11:48:21PM -0400, Frank Z wrote:
> Just looking at Node.js' documentation feels like looking at a dictionary.
> If I used this as my sole guide for making a node.js app, I'd probably
> never even think to use a package.json file, ever... (and what's the point
> if I'm not submitting my app to NPM?).

I think you're right, actually. I saw how this developed, and so it makes sense
to me, but from that perspective, the very API-reference style documentation
leaves a lot to be desired.

Maybe if I get time I'll see if I can write something. Pull requests are
accepted, too -- if you've ideas of what documentation you want to see, I'd be
happy to help shepherd some things.

That said, node's documentation shies away from prescriptivism: It will tell
you what you can do, but not what you should do, since that's a lot more
nebulous and people don't agree on that.

What sort of documentation would you love to see? What's hard as a new user,
and what's obvious?

Aria

Frank Z

unread,
Mar 27, 2014, 11:58:25 PM3/27/14
to nod...@googlegroups.com
Yup and it fails to answer my questions. So as far as I'm concerned, from the documentation, there's no reason to use a package.json unless you are submitting your module to NPM to be downloaded by other people. 


Ryan Schmidt

unread,
Mar 28, 2014, 12:01:54 AM3/28/14
to nod...@googlegroups.com
There is every reason to use package.json, because it is a place to list your npm dependencies so that they can be easily installed, and a place to list scripts that you use to start and test your software.

Now, hopefully that can be the end of this thread. I am not subscribed to this group in order to hear people bicker; I am here to learn interesting technical information about node, so I would appreciate it if we can confine our conversations to that.

po...@pomke.com

unread,
Mar 28, 2014, 12:07:58 AM3/28/14
to nod...@googlegroups.com
I would suggest you go to that link Ben gave you, https://www.npmjs.org/doc/json.html  and read about package.json keys such as 'dependencies', and 'main' specifically. package.json files are not only for projects that are submitted to npm. 


Adam Brady

unread,
Mar 28, 2014, 12:10:36 AM3/28/14
to nod...@googlegroups.com


On Fri, Mar 28, 2014 at 3:07 PM, <po...@pomke.com> wrote:
I would suggest you go to that link Ben gave you, https://www.npmjs.org/doc/json.html  and read about package.json keys such as 'dependencies', and 'main' specifically. package.json files are not only for projects that are submitted to npm. 


Frank Z

unread,
Mar 28, 2014, 12:17:39 AM3/28/14
to nod...@googlegroups.com
"There is every reason to use package.json, because it is a place to list your npm dependencies so that they can be easily installed, and a place to list scripts that you use to start and test your software."

   But if I already installed the dependencies in my node_module folder, what does it matter? 


Frank Z

unread,
Mar 28, 2014, 12:22:12 AM3/28/14
to nod...@googlegroups.com
"See also http://package.json.jit.su/"

Nice.

po...@pomke.com

unread,
Mar 28, 2014, 12:27:48 AM3/28/14
to nod...@googlegroups.com
Repeatability? Suppose you manually install some dep, which turns out to be at version 0.2.5..  and code to that. Then you want to run your code on another machine 3 months later. You npm install foodep and you've got 0.3.1 which has incompatible changes, you don't even remember which version you had previously installed.   By defining your deps in a package.json file you're able to repeatedly deploy and know you've got what you need, at the right version.

Frank Z

unread,
Mar 28, 2014, 12:29:32 AM3/28/14
to nod...@googlegroups.com
"Repeatability? Suppose you manually install some dep, which turns out to be at version 0.2.5..  and code to that. Then you want to run your code on another machine 3 months later. You npm install foodep and you've got 0.3.1 which has incompatible changes, you don't even remember which version you had previously installed.   By defining your deps in a package.json file you're able to repeatedly deploy and know you've got what you need, at the right version."

Ahhhhh okay. Gotcha.


AlainM

unread,
Mar 28, 2014, 12:40:02 AM3/28/14
to nod...@googlegroups.com

I vote for thos to be moderated untill he learns some maners

--
E-mail enviado do meu celular usando Kaiten Mail. Por favor, desculpe minha brevidade.

Ryan Schmidt

unread,
Mar 28, 2014, 12:55:47 AM3/28/14
to nod...@googlegroups.com
In addition to the other reasons mentioned, it has been my experience that when a new version of node is released, especially a new major version, strange errors can occur unless I trash the node_modules folder and reinstall all the modules with that version of node. Using a package.json makes that as easy as “rm -rf node_modules && npm install”.

Jeff Caldwell

unread,
Mar 28, 2014, 1:39:27 AM3/28/14
to nod...@googlegroups.com
You know, Frank, I'm very new to all of this myself but one might have guessed that there's some relationship between how Node handles modules and package.json files. Whoah! It turns out that what Ben linked to was a whole lot of useful information that could help you with learning all about what you were asking (and some things related to it). Specifically http://nodejs.org/docs/latest/api/modules.html#modules_folders_as_modules.

JavaScript, Node and everything that's growing around both is a very complex subject. You need to accept that sometimes information suggested to you won't immediately appear to be relevant to what you're asking. This is my first post to this group even though I've been reading through for the past month,and I'm happy to use it to suggest that you apologize and attempt a new approach to learning and accepting answers to your questions.

Floby

unread,
Mar 28, 2014, 5:24:15 AM3/28/14
to nod...@googlegroups.com
At first, I was angry when stumbling on this post.

Now, I find this funny.

Frank, what is your background ? I think people would grasp your needs better if you explained you come from a PHP backend world, or Java JEE struts world, or whatever. Re-reading your original question made me realise you clearly misunderstood some key concepts about javascript, node.js and UNIX. So i think the answer you were actually looking for was "a node project is not a file, it's a folder". I think you could have figured the rest without much else. I have very few doubts you are not stupid.

However, people here got angry at you not because of your ignorance of the matter, but because of your refusal to even try and your behaviour sometimes resembling that of a 13 years old. It is what makes people angry in general.

I should also note that saying "I'm a lot on stackoverflow and people are helpful" is a lot like saying "I'm a lot on 9gag and they've got all the memes". It's a blatant misunderstanding of how tech knowledge gets created and gathered.

I will finish with my suggestions for your next question :
  • make use of a single question mark per question
  • define what you are trying to achieve as a whole
  • define what you are trying to achieve in particular
  • try stuff
  • define what you tried
  • define how it failed, preferably with a console output
  • show some code example of what you tried
  • be nice to people
Note that that very last point can apply to much broader aspects of your life and will most of the time bring intelligent conversation, because nice people make other people feel safe.

Alex Kocharin

unread,
Mar 28, 2014, 5:52:56 AM3/28/14
to nod...@googlegroups.com
 
28.03.2014, 09:39, "Jeff Caldwell" <nemo...@gmail.com>:
You know, Frank, I'm very new to all of this myself but one might have guessed that there's some relationship between how Node handles modules and package.json files.
 
I consider that relationship a bug. :)
 
package.json is used for npm package manager. Once package is installed, it should work just fine without it.
 
 
--
Reply all
Reply to author
Forward
0 new messages