Why require("perf_hooks") fails? - javascript

In my understanding "perf_hooks" is a part of Node.js. However when testing with npm test it fails for me with the following (some filenames are changed):
Error: ENOENT: no such file or directory, open 'perf_hooks'
at Object.openSync (fs.js:465:3)
at Object.readFileSync (fs.js:368:35)
at SandboxedModule._getCompileInfo (node_modules/sandboxed-module/lib/sandboxed_module.js:265:20)
at SandboxedModule._compile (node_modules/sandboxed-module/lib/sandboxed_module.js:245:22)
at createInnerSandboxedModule (node_modules/sandboxed-module/lib/sandboxed_module.js:183:23)
at SandboxedModule.RecursiveRequireProxy (node_modules/sandboxed-module/lib/sandboxed_module.js:214:27)
at /Users/asmirnov/Documents/blabla2.js:51:12
at Object.<anonymous> (lib/profiler.js:53:3)
at SandboxedModule._compile (node_modules/sandboxed-module/lib/sandboxed_module.js:251:19)
at createInnerSandboxedModule (node_modules/sandboxed-module/lib/sandboxed_module.js:183:23)
at SandboxedModule.RecursiveRequireProxy (node_modules/sandboxed-module/lib/sandboxed_module.js:214:27)
at Object.<anonymous> (lib/blabla1.js:29:20)
at SandboxedModule._compile (node_modules/sandboxed-module/lib/sandboxed_module.js:251:19)
at createInnerSandboxedModule (node_modules/sandboxed-module/lib/sandboxed_module.js:183:23)
at SandboxedModule.RecursiveRequireProxy (node_modules/sandboxed-module/lib/sandboxed_module.js:214:27)
at SandboxedModule.requireInterceptor (node_modules/sandboxed-module/lib/sandboxed_module.js:227:9)
at Context.<anonymous> (test/blabla1.js:73:22)
at processImmediate (internal/timers.js:458:21)
This is a part of the file (lib/profiler.js) that uses it:
const {performance, PerformanceObserver} = (function()
{
try
{
if (window && typeof window.performance == "undefined")
throw new TypeError();
return window;
}
catch (e)
{
return require("perf_hooks");
}
})();
What can be a reason? However in some scenarios (npm run ..) it works as expected. Any suggestions are appreciated.
PS. Node is relatively fresh:
> node --version
v14.8.0

That's a "sandboxed-module" 2.0.3 issue that was fixed in 2.0.4.

perf_hooks is indeed in that version of Node.js. In 14.8.0, require() throws MODULE_NOT_FOUND (and not ENOENT) when it can't find a module, so the problem is something else.
If it is definitely the require() line that is throwing, then it seems likely that require() has been monkey-patched. If they patched require() but not module.require(), you can try changing require() to module.require() in lib/profiler.js.
EDIT: As OP suggests, sandboxed-module is a likely culprit since it messes with module loading. They report that it's fixed in 2.0.4, but based on the repository information on the fix, it would appear to be fixed in 2.0.1. Either way, updating to 2.0.4 (or later, but that's the latest as of this writing) is 👍.

Related

Error: A dynamic link library (DLL) initialization routine failed on electron but it's fine on node js

I'm trying to load a custom module in electron written in D with node_dlang package, which is fine with node, but it fails within electron.
the test with node, that runs just fine, goes like this:
const nativeModule = require('./module.node');
const assert = require('assert');
assert(nativeModule != null);
assert(nativeModule.ultimate() == 42);
But when I went to use it within electron.js, through the preload script, it returns in an error.
the preload script goes like this:
const {
contextBridge,
ipcRenderer
} = require("electron");
const nativeModule = require('./module.node');
const assert = require('assert');
assert(nativeModule.ultimate() == 42);
function pageLoaded()
{
// ...
}
window.addEventListener('DOMContentLoaded', pageLoaded);
the error when I attempt to load the module within electron application is:
A JavaScript error occured in the browser process
--------------------------- Uncaught Exception: Error: A dynamic link library (DLL) initialization routine failed.
\\?\C:\Users\001\Desktop\ele\module.node
at process.func [as dlopen] (VM70 asar_bundle.js:5)
at Object.Module._extensions..node (VM43 loader.js:1138)
at Object.func [as .node] (VM70 asar_bundle.js:5)
at Module.load (VM43 loader.js:935)
at Module._load (VM43 loader.js:776)
at Function.f._load (VM70 asar_bundle.js:5)
at Function.o._load (VM75 renderer_init.js:33)
at Module.require (VM43 loader.js:959)
at require (VM50 helpers.js:88)
at Object.<anonymous> (VM88 C:\Users\001\Desktop\ele\preload.js:6)
What's causing this and how do I fix this?
version
node version is: v14.17.0
electron.js: v13.1.1
both are 64-bit.
the module source code goes like this:
import std.stdio : stderr;
import node_dlang;
extern(C):
void atStart(napi_env env)
{
import std.stdio;
writeln ("Hello from D!");
}
int ultimate()
{
return 42;
}
mixin exportToJs! (ultimate, MainFunction!atStart);
it's compiled with dub command line. No arguments.
UPDATE 1 Do I need to rebuild this module? I found this but it didn't work for me either. I installed the electron-rebuild package by npm install --save-dev electron-rebuild and rebuild with .\node_modules\.bin\electron-rebuild.cmd -v 13.1.1 the command ran fine but I still got same error.
UPDATE 2: inside the console, I clicked in the javascript source code file link in the error message (from the exception) it points to this line of code, where there's this comment saying that:
no static exports found
what does that mean? is this related to the methods in D class? they're marked as public... not sure if related
Electron is a Windows-Application and therefore you need to remove output to std. Try to remove
import std.stdio : stderr;
and
import std.stdio;
writeln ("Hello from D!");
and retry import to Electron.
Please refer this (https://stackoverflow.com/a/74280836/9558119) post from me regarding same. Since it is electron build might be missing Visual C++ Build Environment: Visual Studio Build Tools

React - When debugging in chrome, get a 'document not defined' error

I'm trying to debug a React Native iOS project in Chrome, and I get a stack trace stating document is not defined. When I disable chrome debugging, the app works fine. I'm using react-native 0.14.2 in my package.json. When I try to enable debugging for any of the sample apps, I get the same error in chrome. I simply checked out the react framework from github, so I'm not using the 'react native' commands. Any ideas what could be wrong here? Below is the red box for how my screen looks when 'Chrome Debugging' is enabled. React redbox image below. Thanks!
When I check the the stack trace in chrome debugging this is the line it throws up on:
require("react-native/Examples/ViroSample/ViroSampleApp.ios.js");
Here is the full stack trace:
Document is not definedhandle Exception # ExceptionsManager.js:63
handleError #InitializeJavaScriptAppEngine.js:80
ErrorUtils.reportFatalError # error-guard.js:28
requireImpl # require.js:31
require # require.js:21
(anonymousfunction)#ViroSampleApp.ios.js.js:1
messageHandlers.executeApplicationScript # debuggerWorker.js:18
onmessage # debuggerWorker.js:42
Update
The offending code seems to be the following in ReactErrorUtils.js that is part of the react-haste module:
if (__DEV__) {
/**
* To help development we can get better devtools integration by simulating a
* real browser event.
*/
if (typeof window !== 'undefined' &&
typeof window.dispatchEvent === 'function' &&
typeof Event === 'function') {
var fakeNode = document.createElement('react');
ReactErrorUtils.invokeGuardedCallback = function(name, func, a, b) {
var boundFunc = func.bind(null, a, b);
fakeNode.addEventListener(name, boundFunc, false);
fakeNode.dispatchEvent(new Event(name));
fakeNode.removeEventListener(name, boundFunc, false);
};
}
}
This was a known bug in former (pre 0.14.2) react-native versions, if you recently updated reinstalling the node modules (rm -rf node_modules && npm install) and cleaning the haste cache (rm -rf $TMPDIR/react-*) should fix this.

NodeJs : TypeError: require(...) is not a function

I am trying to require a file and afterwards pass it to a var. I am following this tutorial to create an authentication system. After writing the server.js file and trying to compile I got a BSON error therefore I changed the line that required the release version of it in mongoose.
Here are my code and error:
server.js
require('./app/routes')(app, passport);
Error
require('./app/routes')(app, passport);
^
TypeError: require(...) is not a function
at Object.<anonymous> (d:\Node JS learning\WorkWarV2\server.js:38:24)
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)
at node.js:951:3
Process finished with exit code 1
I have read that this usually means that requireJS is not getting loaded properly yet I am not aware why or how to fix it.
Edit due to comment:
As asked, here is the result of console.log(require);
For me, when I do Immediately invoked function, I need to put ; at the end of require().
Error:
const fs = require('fs')
(() => {
console.log('wow')
})()
Good:
const fs = require('fs');
(() => {
console.log('wow')
})()
I think this means that module.exports in your ./app/routes module is not assigned to be a function so therefore require('./app/routes') does not resolve to a function so therefore, you cannot call it as a function like this require('./app/routes')(app, passport).
Show us ./app/routes if you want us to comment further on that.
It should look something like this;
module.exports = function(app, passport) {
// code here
}
You are exporting a function that can then be called like require('./app/routes')(app, passport).
One other reason a similar error could occur is if you have a circular module dependency where module A is trying to require(B) and module B is trying to require(A). When this happens, it will be detected by the require() sub-system and one of them will come back as null and thus trying to call that as a function will not work. The fix in that case is to remove the circular dependency, usually by breaking common code into a third module that both can separately load though the specifics of fixing a circular dependency are unique for each situation.
For me, this was an issue with cyclic dependencies.
IOW, module A required module B, and module B required module A.
So in module B, require('./A') is an empty object rather than a function.
How to deal with cyclic dependencies in Node.js
Remember to export your routes.js.
In routes.js, write your routes and all your code in this function module:
exports = function(app, passport) {
/* write here your code */
}
For me, I got similar error when switched between branches - one used newer ("typescriptish") version of #google-cloud/datastore packages which returns object with Datastore constructor as one of properties of exported object and I switched to other branch for a task, an older datastore version was used there, which exports Datastore constructor "directly" as module.exports value. I got the error because node_modules still had newer modules used by branch I switched from.
I've faced to something like this too.
in your routes file , export the function as an object like this :
module.exports = {
hbd: handlebar
}
and in your app file , you can have access to the function by .hbd
and there is no ptoblem ....!
I don't know how but in may case it got fixed when I changed
require('./routes')(app)
to
require('./routes')
In my case i fix when i put the S in the module.exportS,
BEFORE:
module.export = () => {}
AFTER:
module.exports = () => {}

How to use node-youtube-dl driver?

I use youtube-dl node driver to get information about videos on youtube, but I have trouble with ReferenceError - require is not defined (First line). I installed node-youtube-dl module on my laptop and the code is just taken from example in description. Is there anything else that I'm missing to do?
var youtubedl = require('youtube-dl');
var url = 'https://www.youtube.com/watch?v=9Q7Vr3yQYWQ';
ytdl.getInfo(url, function(err, info) {
if (err) throw err;
console.log('id:', info.id);
console.log('title:', info.title);
console.log('url:', info.url);
console.log('thumbnail:', info.thumbnail);
console.log('description:', info.description);
console.log('filename:', info._filename);
console.log('duration:', info.duration);
console.log('format_id:', info.format_id);
});
require() is a keyword in Node.js. It is used to import CommonJS modules like those in the npm registry. But it is not a keyword in your browser, where trying to use it will return a ReferenceError as you describe.
If you want to import npm modules using require() in your browser, you can try browserify or webpack. However, it is entirely possible (probable?) that youtube-dl is not compatible with browserify or webpack.
If you don't need to run it from a browser, you can put the code in a file (say, myFile.js) and run it from the command line with node:
node myFile.js
In your sample:
var youtubedl = require('youtube-dl');
However later you call it:
ytdl.getInfo(url, function(err, info)
just simply rename your require to ytdl, like this:
var ytdl = require('youtube-dl');

Meteor: TypeError: Object #<Object> has no method 'require'

I had problems previously trying to get the Meteor.require method to work and I thought it was fixed but apparently not. Although I have since upgraded to Meteor 0.9.0.
Anyone have any idea how I can get the Meteor.require method to work?
Here's what I get on the terminal when passing params back to the server
=> App running at: http://localhost:3000/
I20140827-11:08:21.949(-7)? createImage
I20140827-11:08:21.953(-7)? 24890
I20140827-11:08:22.030(-7)? Exception while invoking method 'createImage' TypeError: Object #<Object> has no method 'require'
I20140827-11:08:22.032(-7)? at Meteor.methods.createImage (app/server/server.js:7:21)
I20140827-11:08:22.033(-7)? at maybeAuditArgumentChecks (packages/livedata/livedata_server.js:1492)
I20140827-11:08:22.034(-7)? at packages/livedata/livedata_server.js:643
I20140827-11:08:22.034(-7)? at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20140827-11:08:22.034(-7)? at packages/livedata/livedata_server.js:642
I20140827-11:08:22.034(-7)? at _.extend.withValue (packages/meteor/dynamics_nodejs.js:56)
I20140827-11:08:22.034(-7)? at _.extend.protocol_handlers.method (packages/livedata/livedata_server.js:641)
I20140827-11:08:22.035(-7)? at packages/livedata/livedata_server.js:541
This is my Meteor.method:
createImage: function(coords) {
console.log('createImage')
console.log(coords.area);
var gd = Meteor.require('node-gd');
},
my packages.json file:
{
"node-gd":"0.2.3"
}
node-gd is definitely there too:
me#ubuntu:~/leaderboard/packages/npm/npm/node_modules/node-gd$ ls
binding.gyp build coffee cpp js package.json README.md
Meteorhacks just released (I'm talking about 4 hours ago) the updated npm package for Meteor 0.9, it's called meteorhacks:npm https://github.com/meteorhacks/npm
You should remove the previous package which I suppose is broken (something like arunoda:npm I guess) and give the new one a try.
Be aware that Meteor.require has been replaced by Meteor.npmRequire !

Categories

Resources