How do I enable uncaught exception traces from within a node script? - javascript

Node can print an uncaught exception trace when it exits due to an uncaught exception. This feature is enabled when I pass --trace-uncaught to node.
How do I enable this feature from the source code from within my script?
Perhaps something along the lines of node.trace_uncaught = true?

I haven't found a general solution to this yet. For now, I'll just (ab)use linux and sh to achieve what I want by adding this line to the top of my script:
//bin/true; exec /usr/bin/env node --trace-uncaught "$0" "$#"
This line uses sh to launch node with the desired flag. I'll leave this question open in case someone posts a proper cross-platform solution.

Related

Executing jest test statements from the node repl

Is there any way to execute jest test statements line by line (either using a jest setting or through the node repl)?
you need to use node debugging, like: https://nodejs.org/en/docs/guides/debugging-getting-started/ . Then, you can set a breakpoint at the first line of your test and step through from there.
Visual Studio code has a debugger that makes this easy, and you can use extensions like Ora's popular jest extension to auto run all your tests and add a link to easily launch debugging on any failing test.

What it means "require is not defined" in JS and how the libraries are available for the browser?

I installed through npm in WebStorm this library
npm install basicscroll
The IDE highlights the words of the package, and everything seems fine until I try to see the project in the browser:
The console gives me this issue:
applicative.js:15 Uncaught ReferenceError: basicScroll is not defined
at applicative.js:15
at NodeList.forEach (<anonymous>)
at applicative.js:6
So I added at the beginning of my JS file this statement:
const basicScroll = require('basicscroll')
The result this time is that the library remains highlighted, but the method called on it not. And in browser this is the error prompt by the console:
Uncaught ReferenceError: require is not defined
I checked my node_modules directory, I can see the sub-directory, so I think the installation process went right. I also checked the package.json file:
"dependencies": {
"basicscroll": "^3.0.2",
[...]
I honestly don't know where the problem is. I'm new to web development and online I found that the webpack module-bounder could help me, but I don't even know where to start. What is wrong ?
Thank'you !
It seems like you are trying to use server-side code in a client-side environment. As far as I know require is a NodeJS thing and doesn't exist in the browser.
Your easiest solution would be to use a <script> tag in your html to import the files you need. Something like <script src="dist/basicScroll.min.js"></script>
This answer mentions some other tools you could use to manage that sort of thing: https://stackoverflow.com/a/19059825/1801137

DOMException error creating AudioWorlet with Webpack

I got an error when trying to create an AudioWorklet, but only when the project is served through webpack-dev-serve. If I serve the files directly with an http server, everything works fine. So I think it must be something related with Webpack configuration.
This is the error I get:
audio-meter.js:19 Uncaught (in promise) DOMException: Failed to construct 'AudioWorkletNode': AudioWorkletNode cannot be created: The node name 'audio-meter' is not defined in AudioWorkletGlobalScope.
at new AudioMeterNode (http://localhost:9999/bundle.js:9421:17)
at Function.create (http://localhost:9999/bundle.js:9430:16)
I've been searching and there are some related questions, but none of them helped me to solve the problem. One of them points to a Chromium bug, but in this case the error only happens when the project is served through Webpack (I'm using worker-loader plugin):
AudioWorklet DOMException error when loading modules
AudioWorklet error: DOMException: The user aborted a request
Use AudioWorklet within electron (DOMException: The user aborted a request)
I've set up here a branch with the most simplified version of the project I've could, there are some files involved so I think you could see it better this way. To get it, please: git clone git#bitbucket.org:alvaro_maceda/notoono.git and git checkout stackoverflow.
If you run it with npm start it will use Webpack and, when you press the "start" button, you will see the error on the console. If you comment audio-meter.js:11 and uncomment audio-meter.js:11 and index.html:14 you can run it with npm start http and everything will go fine.
Could anybody give an idea about what to search or where the error could be?

Running selenium with Javascript on Node.js

I am a noob to Javascript so apologies if my question is very trivial.
I am trying to run a selenium test that was wrote in Javascript. As I usually do, I just want to start with something simple and work from there. In my script I am just trying to load Google using chromedriver.
var webdriver = require("selenium-webdriver");
var driver = new webdriver.Builder().withCapabilities(webdriver.Capabilities.chrome()).build();
driver.get("http://www.google.com");
On the CLI I navigate to the directory where the Test.js file is saved in and I run the command node Test.js. I always get this error in response;
C:\Selenium\node_modules\selenium-webdriver\_base.js:104
vm.runInContext(opt_srcText, closure, src);
^
SyntaxError: Unexpected token )
at goog.loadModuleFromSource_ (C:\Selenium\node_modules\selenium-webdriver\l
at Object.goog.loadModule (C:\Selenium\node_modules\selenium-webdriver\lib\g
at C:\Selenium\node_modules\selenium-webdriver\lib\webdriver\promise.js:1:6
at Object.Context.closure.goog.retrieveAndExecModule_ (C:\Selenium\node_modu
at <anonymous>:1:6
at Context.closure.closure.vm.createContext.CLOSURE_IMPORT_SCRIPT (C:\Seleni
at Object.goog.importScript_ (C:\Selenium\node_modules\selenium-webdriver\li
at Object.goog.importModule_ (C:\Selenium\node_modules\selenium-webdriver\li
at Object.goog.writeScripts_ (C:\Selenium\node_modules\selenium-webdriver\li
at Object.goog.require (C:\Selenium\node_modules\selenium-webdriver\lib\goog
I originally ran this code on my Windows machine and when I was getting that error I put it down to Windows and Node.js no agreeing and tried it on my Mac. Still no luck as I was getting the exact same response.
On both machines I have node and npm installed. Previous to executing the tests I ran the command npm install selenium-webdriver and I also added chromedriver to my PATH.
I have no idea what I am doing wrong so if anyone can point me in the right direction, it'd be very much appreciated.
Turns out the version of node I was using was too old.
Thanks to #Louis for your help in reaching this solution.
What I did was uninstall node and re-installed it with the latest version. I would imagine upgrading would work too.

Installing Nodejitsu

I have successfully installed NodeJitsu using NPM. However, anytime I attempt to run it I get the following error:
TypeError: Arguments to path.join must be strings.
It happens at line 204 in path.js
Any ideas?
We are working on release our toolset with node 0.10.0 support, you can use it with the last 0.8.x branch (0.8.22). We will publish a new version as soon as possible.
This issue is fixed internally but we are working on all the possible points of failure in node 0.10.0.
Stay tunned for updates!
If you need support you can join #nodejitsu on Freenode ;)

Categories

Resources