Best way to read package.json from the code within a package.

6,776 views
Skip to first unread message

Chris Dew

unread,
Jul 18, 2011, 8:56:21 AM7/18/11
to nodejs
I have a module which needs to refer to it's own version number (and
other details) from within its code (for a welcome banner).

I am currently using:

var packageJson = JSON.parse(fs.readFileSync('./package.json'));

This will break once this package is required by another.

Is there a common solution for this?

Thanks,

Chris.

Aseem Kishore

unread,
Jul 18, 2011, 8:58:54 AM7/18/11
to nod...@googlegroups.com
Yep, you can use __dirname within your own package. That will point to your package regardless of the process's current working directory.

Cheers,
Aseem

--
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

Nathan Rajlich

unread,
Jul 18, 2011, 11:32:04 PM7/18/11
to nod...@googlegroups.com

Marak Squires

unread,
Jul 18, 2011, 11:38:26 PM7/18/11
to nod...@googlegroups.com
+1

Chris Dew

unread,
Jul 19, 2011, 6:08:48 AM7/19/11
to nodejs
Thanks,

Chris.

On Jul 19, 4:32 am, Nathan Rajlich <nat...@tootallnate.net> wrote:
> https://github.com/indexzero/node-pkginfo
>

Nik Sumeiko

unread,
May 26, 2014, 5:43:19 AM5/26/14
to nod...@googlegroups.com
Why not just use `var packageJson = require(__dirname + '/package.json');` to get configuration object without parsing.

Matt

unread,
May 26, 2014, 12:11:53 PM5/26/14
to nod...@googlegroups.com

On Mon, May 26, 2014 at 5:43 AM, Nik Sumeiko <sumeiko...@gmail.com> wrote:
Why not just use `var packageJson = require(__dirname + '/package.json');` to get configuration object without parsing.

How is require() not parsing?

Nik Sumeiko

unread,
May 26, 2014, 1:19:23 PM5/26/14
to nod...@googlegroups.com
With `require` you get the JSON object; With `fs.readFileSync` you get a string that have to be parsed using `JSON.parse`.

So `var pkg = require(__dirname + '/package.json')` is much shorter than `var pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json'))`, especially when you cannot use `fs` module without loading it before with `var fs = require('fs')`

Aria Stewart

unread,
May 26, 2014, 2:08:21 PM5/26/14
to nod...@googlegroups.com

On May 26, 2014, at 1:19 PM, Nik Sumeiko <sumeiko...@gmail.com> wrote:

> With `require` you get the JSON object; With `fs.readFileSync` you get a string that have to be parsed using `JSON.parse`.
>
> So `var pkg = require(__dirname + '/package.json')` is much shorter than `var pkg = JSON.parse(fs.readFileSync(__dirname + '/package.json'))`, especially when you cannot use `fs` module without loading it before with `var fs = require('fs')`

And more saliently, require() resolves paths differently, and in this case, I think with behavior that’s wanted.

Aria
signature.asc

Stefano Baghino

unread,
May 27, 2014, 12:11:34 AM5/27/14
to nod...@googlegroups.com
At one point or another, someone is parsing that JSON. Are you sure that using require() this way is idiomatic enough for people to understand it when reading the code? I believe it would feel more natural if he explicitly opens the file and parses its content. The only improvement I'd suggest would be to make it async, if possible.

Paul Vencill

unread,
May 27, 2014, 8:15:39 AM5/27/14
to nod...@googlegroups.com
Yep, agree with Arya and Nick, using require() is the best way to do it. I've used it on several modules to good effect.

Darren DeRidder

unread,
May 28, 2014, 1:28:18 AM5/28/14
to nod...@googlegroups.com
@Nic that question was 2011 and its now pretty standard to require('file.json') ever since support for that was added (by isaacs if memory serves). Ah, here it is https://github.com/joyent/node/issues/1357

You can require the json, freeze the object to get read only, and wrap a little module around it with some convenience methods...

Francesco Mari

unread,
May 28, 2014, 9:19:46 AM5/28/14
to nod...@googlegroups.com

There is no absolute best way to do that.

require caches the content of the file after the first access. Calls to require after the first one will just return the cached content, and the file will not be parsed again. Use this approach if you expect that the content of the file never change during the execution time of your application.

If you read and parse the file manually you are in charge of controlling if you want to reload the content every time you access it or if you want to cache its content. In addition, programmatically reloading the file doesn't necessarily require your application to be restarted.

If you choose the manual approach, it may not be required to access the file asynchronously. If the file contains configuration information that are needed to start your main application you can also use synchronous code. There is usually no "concurrency" to handle during the startup phase of your application.

Il 27/mag/2014 16:30 "Paul Vencill" <paul.v...@gmail.com> ha scritto:
Yep, agree with Arya and Nick, using require() is the best way to do it. I've used it on several modules to good effect.


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.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/a9d6ca81-30ea-43ea-b7da-7c9113ae94dc%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt

unread,
May 28, 2014, 9:19:50 AM5/28/14
to nod...@googlegroups.com
On Tue, May 27, 2014 at 12:11 AM, Stefano Baghino <k0b...@gmail.com> wrote:
At one point or another, someone is parsing that JSON. Are you sure that using require() this way is idiomatic enough for people to understand it when reading the code? I believe it would feel more natural if he explicitly opens the file and parses its content.

I agree.
 
The only improvement I'd suggest would be to make it async, if possible.

That's not important if done once at startup time, when everything is loaded sync anyway. It's only relevant if done inside an API call.

Matt.
Reply all
Reply to author
Forward
0 new messages