I made a CLI tool with node.js that is working fine. Then I wanted to publish it as a npm package which worked fine too. But now there is the following error since a (local) file cannot be found:
/usr/lib/node_modules/german-bible-cli/app.js:8 if (err) throw err; ^ [Error: ENOENT: no such file or directory, open './schlachter.json'] { errno: -2, code: 'ENOENT', syscall: 'open', path: './schlachter.json' }
The error happens after I install the published package and try running the following command:
$ german-bible-cli
The project can be found here. Thanks for helpin' me :)) ✝
Edit: My package.json does have the bin
field specified as follows, although I haven't yet uploaded the current version to GitHub.:
"bin": "app.js"
Make the following two modifications to your app.js
file:
Change line number 7 from this:
fs.readFile("./schlachter.json", "utf8", (err, data) => {
to this instead:
fs.readFile(path.resolve(__dirname, "./schlachter.json"), "utf8", (err, data) => {
Import the path
module by adding the following require
statement at the top of the file:
const path = require("path");
Explanantion:
The aforementioned change utilizes the builtin path
modules resolve()
method to resolve the following two path segments:
__dirname
- the path of the directory that contains your module../schlachter.json
- the relative path.This essentially forms an absolute path to the schlachter.json
file location.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments