JoeTidee
JoeTidee

Reputation: 26104

How to load a user-specific file from within a node module?

I am creating a node module that applications can import (via npm install). A function within my module will accept the location of a .json file that is set in the users' application (as specified by filePath below):

...
function (filePath){
    messages = jsonfile.readFileSync(filePath);
}
...

How do I allow my function to accept this file path and process it in a way that my module will be able to find it, given that my function will never know where the users' application file will be stored?

Upvotes: 2

Views: 65

Answers (2)

kinolee
kinolee

Reputation: 53

how about use absolute path ?

if you write in yourapp/lib/index.js.

path.join(__dirname, '../../../xx.json');

Upvotes: 0

Rico Kahler
Rico Kahler

Reputation: 19292

If you're writing a node library then your module will be required by the user's application and thus saved in the node_modules folder. The thing to notice is that your code just becomes code run in the user's application, therefore paths will be relative to the user's application.

For example: Let's make two modules, echo-file and user-app with their own folders and their own package.jsons as their own projects. Here is a simple folder structure with two modules.

workspace
|- echo-file
  |- index.js
  |- package.json
|- user-app
  |- index.js
  |- package.json
  |- userfile.txt

echo-file module

workspace/echo-file/package.json

{
  "name": "echo-file",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {"test": "echo \"Error: no test specified\" && exit 1"},
  "author": "",
  "license": "ISC"
}

workspace/echo-file/index.js (the entry point of your module)

const fs = require('fs');
// module.exports defines what your modules exposes to other modules that will use your module
module.exports = function (filePath) {
    return fs.readFileSync(filePath).toString();
}

user-app module

NPM allows you to install packages from folders. It will copy the local project into your node_modules folder and then the user can require it.

After initializing this npm project, you can npm install --save ../echo-file and that will add it as a dependency to the user's application.

workspace/user-app/package.json

{
  "name": "user-app",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {"test": "echo \"Error: no test specified\" && exit 1"},
  "author": "",
  "license": "ISC",
  "dependencies": {
    "echo-file": "file:///C:\\Users\\Rico\\workspace\\echo-file"
  }
}

workspace/user-app/userfile.txt

hello there

workspace/user-app/index.js

const lib = require('echo-file'); // require
console.log(lib('userfile.txt')); // use module; outputs `hello there` as expected

How do I allow my function to accept this file path and process it in a way that my module will be able to find it, given that my function will never know where the users' application file will be stored?

So long story short: file paths will be relative to the user's app folder.

When your module is npm installed, it copies to node_modules. When a file path is given to your module, it will be relative to the project. Node follows the commonJS module definition. EggHead also has a good tutorial on it.

Hope this helps!

Upvotes: 1

Related Questions