0

I am building an Electron application and I am using webContents.executeJavaScipt() which is pretty much a eval() for the Electron browser. I never had a problem until we put the project on my clients Windows computer and this is the error:

module.js:472 Uncaught Error: Cannot find module 'C:UsersMichael Bruce AllenDocumentsGitHubschedule-crawl
enderer
emoteItems.js'
    at Module._resolveFilename (module.js:470:15)
    at Function.Module._resolveFilename (C:\Users\Michael Bruce Allen\Documents\GitHub\schedule-crawl\node_modules\electron\dist\resources\electron.asar\common\reset-search-paths.js:35:12)
    at Function.Module._load (module.js:418:25)
    at Module.require (module.js:498:17)
    at require (internal/module.js:20:19)
    at <anonymous>:3:25
    at EventEmitter.electron.ipcRenderer.on (C:\Users\Michael Bruce Allen\Documents\GitHub\schedule-crawl\node_modules\electron\dist\resources\electron.asar\renderer\init.js:52:28)
    at emitMany (events.js:127:13)
    at EventEmitter.emit (events.js:201:7)

To me it seems like an escaping issue to me. So I broke this down as simple as possible and I would like to know what is happening here:

const path = require('path');

const projectPath = `
  console.log('${path.join(__dirname, "project_path")}');
`;

eval(projectPath);

I get on WINDOWS: (clearly wrong)

C:UsersMichael Bruce AllenDocumentsGitHubsandboxproject

On LINUX: (Looks beautiful)

/home/codeamend/Coding/projects/work/upwork/schedule-crawl/journal/learning/project_path

Michael Bruce
  • 10,567
  • 2
  • 23
  • 31

1 Answers1

0

Well, it turns out that sometimes you just need to know what types of keywords to use to search Google and find your answer.

What was happening is the Windows \ was escaping characters. Even though I knew something like this was happening, I didn't understand why.

It turns out that this stackexchange post helped me find the answer.

This is not as pretty as I want, so I will refactor my actual project into another solution that is less hacky.

const path = require('path');

const projectPath = `
  console.log(${JSON.stringify(path.join(__dirname, "project_path"))});
`;

eval(projectPath);
Community
  • 1
  • 1
Michael Bruce
  • 10,567
  • 2
  • 23
  • 31