Issue with node fs.readFileSync() and cron - node.js

I'm trying to run a node script with Crone and even though I think I'm using the absolute path in every step of the way, I am still getting a "no such file or directory" when trying to read a JSON file while running the script from Cron.
The interesting thing is that everything works fine when I run it from the shell!
[/]$ /usr/local/bin/node /root/Main/email.js
^^ This works fine!
However when I run with crontab like this:
0,29 * * * * /usr/local/bin/node /root/Main/email.js
I get this printed to the log:
fs.js:438
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory 'root/Main/email.json'
at Object.fs.openSync (fs.js:438:18)
at Object.fs.readFileSync (fs.js:289:15)
at Object.<anonymous> (/root/Main/email.js:11:15)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
The line of code on my node sketch that is throwing the error is this one:
var file = fs.readFileSync('root/Main/email.json', 'utf8')
What am I missing? Isn't this the absolute path? Why is it only working directly from the shell!

Related

How to resolve Sourcetree node path issues after nvm setup?

Sourcetree starting throwing error as below after having nvm setup with multiple versions of node. Sourcetree unable to find the exact nodepath.
How to set default nodePath to target for Sourcetree?
Info: can't find node in PATH, trying to find a node binary on your system
/Users/project-ui/node_modules/husky/run.js:8
message(requiredVersion) {
^
SyntaxError: Unexpected token (
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
Completed successfully
I managed with couple of steps
which node: /Users/xyz/.nvm/versions/node/v12.18.2/bin/node
I included that to path variable inside bash_profile:
export PATH="/Users/xyz/.nvm/versions/node/v12.18.2/bin:$PATH"
Worked like charm

how can i fix this error that nodemon keep on showing

i following an express.js tutorial andd after some code and functionalities with api requests i followed exactly how he changed the code that handles these requests into a seperate js file in another folder just to clean up the code but after i did that this error showed up
i tried changing the file name , i tried removing the node-modules folder and running npm install again and i tried installing the module with the issue
internal/modules/cjs/loader.js:638
throw err;
^
Error: Cannot find module '/routes/api/members'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:690:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object.<anonymous> (C:\Users\Get_Rico\Desktop\crashcoursse\index.js:11:25)
at Module._compile (internal/modules/cjs/loader.js:776:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
[nodemon] app crashed - waiting for file changes before starting...
i expect nodemon to save the changes and work just fine with no errors just like what happened in the tutorial
It appears the error is probably coming from this line of code and it's telling you that it can't find the '/routes/api/members' module file.
And, this would not be working because you're trying to load a local module without a proper path name. If no path is specified, then it tries to load a global or built-in module. If that module is in the current directory, then you should use the correct path:
const yourModule = './routes/api/members';

app-root-path doesn't return absolute path correctly on linux?

I'm building an os-agnostic npm package that uses a few submodules from within itself. On my mac, I require modules using app-root-path like so:
var path = require('path');
var appRoot = require('app-root-path');
var packageJson = JSON.parse(fs.readFileSync(path.join(appRoot.path, 'node_modules', 'myModule', 'package.json')).toString());
and this works as expected.
But it fails on linux (and I assume it'd fail on windows too) because the app-root-path package above doesn't return correct path the same way it does on mac.
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open '/usr/local/lib/node_modules/myModule/bin/node_modules/myModule/package.json'
at Error (native)
at Object.fs.openSync (fs.js:584:18)
at Object.fs.readFileSync (fs.js:431:33)
at Object.<anonymous> (/usr/local/lib/node_modules/myModule/bin/commands.js:247:33)
at Module._compile (module.js:425:26)
at Object.Module._extensions..js (module.js:432:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:313:12)
at Function.Module.runMain (module.js:457:10)
at startup (node.js:138:18)
Why is this?
How can I make it work without worrying about host OS (mac / linux / windows)?
Note, I did use the answers/tips on this question on stackoverflow.

How generate r.js using node dist.js

In the requireJS site they said that We can generate the r.js using
"node dist.js"
Source http://requirejs.org/docs/node.html
for me it does make error :
node git:(develop) ✗ node dist.js
fs.js:549 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT: no such file or directory, open 'build/jslib/x.js'
at Error (native)
at Object.fs.openSync (fs.js:549:18)
at Object.fs.readFileSync (fs.js:397:15)
at Object.<anonymous> (/Users/mtaboubi/www/capsana-portal/webapp/unified-portal/src/main/webapp/node/dist.js:21:19)
at Module._compile (module.js:434:26)
at Object.Module._extensions..js (module.js:452:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Function.Module.runMain (module.js:475:10)
at startup (node.js:117:18)
Any help ? thank you
Sounds like there might an error in your version of dist.js / fs.js.
Could you try running node dist.js on the original version of the r.js repository here?
If that works, then you'll have to do further troubleshooting on your r.js file to get to the bottom of the issue.
Otherwise, it's an issue with your development environment, in which case you can post the errors here.

can't import modules into a node script if it runs from cron

I have the module underscore installed globally with npm. if I run the script
/usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js
it will run ok wherever I am on the path, but if I run a cronjob like this:
3,18,33,48, * * * * /usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js
I get this error:
Date: Wed, 10 Sep 2014 16:26:01 -0600
From: Cron Daemon <root#db.local>
To: olmo#db.local
Subject: Cron <olmo#db> /usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js
module.js:340
throw err;
^
Error: Cannot find module 'underscore'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:364:17)
at require (module.js:380:17)
at Object.<anonymous> (/home/olmo/sandbox/api_ievwebapp/parseAdminScripts/processDrivesMultiUser.js:20:9)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
somehow it can't find the module underscore if it runs via cron. I made the cron entry for the same username I'm using to run the script manually.
any ideas?
I could find the answer based on the comment by mu is too short.
I modified the crontab to include the environmental variable NODE_PATH
3,18,33,48, * * * * export NODE_PATH=/usr/local/lib/node_modules/ && /usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js
now I'm able to use modules in node.js if called by cron.

Resources