question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Local scripts won't run at all on Mac

See original GitHub issue

I’m using the following code on both a Windows machine and a Macbook in Node.js environment (with no module loaders of any kind):

var jsdom = require('jsdom');

module.exports = function(url, inject, injectPath) {

  var jsdomConfig = {};

  jsdomConfig.url = url;
  jsdomConfig.scripts = [`${__root}lib/jquery.js`];

  if ( inject ) jsdomConfig.scripts.push(injectPath);

  jsdomConfig.done = function(error, window) {

    // Expecting all the scripts to be executed
    console.log(window.$ === undefined); // Prints true

  };

  jsdom.env(jsdomConfig);

};

_root resolves to the project path (example: C:/myproject/).

The code runs perfectly on my Windows machine and the console.log prints false. However, on my Macbook it prints true.

I’ve tried all the suggested fixes (waiting for window to finish loading, etc.) but as long as the scripts are local, they never run on my Macbook.

I tried using a URL to load jQuery and it works! When I change the 5th line to jsdomConfig.scripts = ['https://code.jquery.com/jquery-3.1.1.min.js']; the console.log prints false in both my Macbook and the Windows machine. So I came up with a workaround (since the code is part of an Express.js webservice) and added a route to serve my local files using res.download() method. So instead of adding the local path of jQuery, I used something like http://localhost:8000/jquery and got it loaded when done() is called. Used the same method for other scripts passed to the function and they all work too.

But this adds unnecessary time to the whole process and I assume the downloaded file should be saved temporarily somewhere, so it occupies unnecessary space.

window will be a fully-loaded window, with all external resources downloaded and <script>s executed.

…quoting from the documentation. Clearly that is not happening!

Any ideas why local scripts are not executed when done() is called as opposed to non-local scripts and why is it Mac specific?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
domeniccommented, Apr 29, 2017

Be sure to try the new v10 API documented in the readme, which should make things much clearer! There you can inject script tags the normal way, using document.createElement("script") for example.

2reactions
Sebmastercommented, Mar 12, 2017

But why does it work on Windows when I provide a local path?

That might actually be a bug. It seems like we’re assuming that the protocol in that URL is “C:” and the rest is a data-segment and then at a lower level we just check if a file on that path exists (which will return false for all http:// URLs). Haven’t checked if that’s actually the case though.

Would file:///Users/ramtin/myproject/lib/jquery.js work on Mac?

Yes, it should.

Read more comments on GitHub >

github_iconTop Results From Across the Web

scripts in /usr/local/etc/periodic don't run - Apple Discussions
Need to add a local periodic script, and so created /usr/local/etc/periodic, chmod'd to 755, and added my script. It doesn't run.
Read more >
Local Script won't run - DevForum | Roblox
The problem is in the local script, where the green/red parts will be touched, but it won't fire the remote events and no...
Read more >
Make a Mac .App run a script on running of said .App
Run Automator and create a new Application project. Add a Run Shell Script action. In the Shell: list, select the interpreter of choice; ......
Read more >
Fix Terminal “Operation not permitted” Error in macOS ...
First you could try emptying the trash securely. [Finder->Secure Empty Trash…]. If that doesn't work, go to the terminal and navigate to the ......
Read more >
Script Errors (What They Are and How to Fix Them) - Lifewire
Turning off scripting errors will stop you from seeing them, but doesn't necessarily mean that the scripts themselves will work properly just ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found