Anyone up on ES6/Node.js?

Something weird with the require command. Maybe someone knows what’s going on here?
I literally check that the file exists the line before, and still Node complains the file does not exist! (Or that’s what it seems!)

if (fs.existsSync('../newParamMsgs.json')) {
		newParamMsgs = require('../newParamMsgs.json');
		fs.copyFile(fname, fname + '.bak', (err) => {
			if (err) {
				console.log(`Cant open file ${fname}!`);
			}
		});
} else {
		console.log(`${fname} does not exist.`);
}

Error message:

Error: Cannot find module '../newParamMsgs.json'
    at Function.Module._resolveFilename (module.js:548:15)
    at Function.Module._load (module.js:475:25)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at Object.setFName (/Users/andyb/Dropbox/Documents/companion/companion/lib/module/yamaha-MIDI/yamaha.js:22:18)
    at fs.access (/Users/andyb/Dropbox/Documents/companion/companion/lib/module/yamaha-MIDI/index.js:110:12)
    at FSReqWrap.oncomplete (fs.js:135:15)

Are you pointing to the directory you think you are pointing to?

How would I be able to tell?
These 2 lines are the “same”, so why is the first ok, the 2nd not?

if (fs.existsSync('../newParamMsgs.json')) {
newParamMsgs = require('../newParamMsgs.json');

Does “require” use a different folder starting point than fs?

I guess they start at different root folders, but I can’t find any documentation on that!
the solution is
folder = require('path').resolve('../');
then

if (fs.existsSync(`${folder}newParamMsgs.json`))
    newParamMsgs = require(`${folder}newParamMsgs.json`);

I tried to find some documentation too but found nothing. I’m no Node expert, I let Express do the thinking for me in the limited things I’ve done with it

1 Like