npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

set-property-from-file

v2.0.0

Published

Set object property using a file path and contents

Readme

set-property-from-file

Build Status Build status Coverage Status Dependency Status devDependency Status

Set object property using a file path and contents

var setPropertyFromFile = require('set-property-from-file');

// foo.txt (Hello!)

setPropertyFromFile({bar: true}, 'foo.txt', 'utf8', function(err, res) {
  if (err) {
    throw err;
  }

  res; //=> {foo: 'Hello!', bar: true};
});

Installation

NPM version

Use npm.

npm install set-property-from-file

API

var setPropertyFromFile = require('set-property-from-file');

setPropertyFromFile(target, filePath [, options], callback)

target: Object
filePath: String (a relative file path)
options: Object or String (file encoding)
callback: Function

It reads a file asynchronously, then sets the property of the target object to the file contents. (It automatically strip UTF-8 byte order mark from contents.)

The names of the created properties are based on the file path. For example,

  • foo.txt sets foo property.
  • foo/bar.txt sets foo.bar property.
  • foo/bar/baz.qux.txt sets foo.bar['baz.qux'] property.
  • ../foo/bar.txt sets ['..'].foo.bar property.
  • foo/../bar/baz.txt sets bar.baz property.
var assert = require('assert');
var setPropertyFromFile = require('set-property-from-file');

var target = {
  fixtures: {
    foo: 'bar'
  }
};

setPropertyFromFile(target, 'fixtures/images/00.jpg', function(err, res) {
  if (err) {
    throw err;
  }
  
  // Adds fixtures.images['00'] property to the target object.
  assert.deepEqual(res, {
    fixtures: {
      foo: 'bar' // target's default property
      images: {
        '00': <Buffer ... > // new property
      }
    }
  });
});

It doesn't create a new property under the existing property if the existing property is not an object.

var assert = require('assert');
var setPropertyFromFile = require('set-property-from-file');

var target = {
  fixtures: {
    foo: 'Hello!' // string (not an object)
  }
};

setPropertyFromFile(target, 'fixtures/foo/bar/baz.txt', function(err, res) {
  if (err) {
    throw err;
  }

  // Doesn't overwrite the existing non-object property.
  assert.deepEqual(res, {
    fixtures: {
      foo: 'Hello!' // object['fixtures']['foo'] already exists
    }
  });
});

options

(In addition to the follwing options, all fs.readFile options are available.)

options.cwd

Type: String
Default: process.cwd()

Specify the working directory the source path is relative to. This won't be included in the property names.

// Reads one/two/three.txt
setPropertyFromFile({}, 'two/three.txt', {cwd: 'one'}, function(err, res) {
  res; // {two: {three: <Buffer ...>}}
});
options.base

Type: String
Default: ''

Specify the directory relative to the cwd. This won't be included in the property names.

// Reads one/two/three.txt
setPropertyFromFile({}, 'one/two/three.txt', {base: 'one/two'}, function(err, res) {
  res; // {three: <Buffer ...>}
});
options.ext

Type: Boolean
Default: false

By default the property names don't include file extension. true keeps it in the last property name.

setPropertyFromFile({}, 'index.js', function(err, res) {
  res; //=> {index: ... }
});

setPropertyFromFile({}, 'index.js', {ext: true}, function(err, res) {
  res; //=> {index: {js: ... }}
});
options.processor

Type: Function
Default: undefined

Specify a function to process file contents before setting the properties.

// foo.txt (abcde)

var encoder = function(content) {
  return content.toString().toUpperCase();
};

setPropertyFromFile({}, 'foo.txt', {encoding: encoder}, function(err, res) {
  res; //=> 'ABCDE'
});

callback(error, result)

error: Error if it fails to read the file, otherwise null
result: Object (Target object modified)

Note that it overwrites the target object.

var target = {foo: true};

setPropertyFromFile(target, 'bar.txt', function(err, res) {
  res; //=> {foo: true, bar: ... }
  target; //=> {foo: true, bar: ... }
  res === target; //=> true
});

License

Copyright (c) 2014 Shinnosuke Watanabe

Licensed under the MIT License.