2

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?

1
  • 1
    Require it to be a full path? The application can just pass path.join(__dirname, …). Commented Feb 20, 2017 at 4:17

2 Answers 2

1

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!

Sign up to request clarification or add additional context in comments.

Comments

0

how about use absolute path ?

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.