nodejs - jade ReferenceError: process is not defined - node.js

the projcet is generated by webstorm's express template.
the npm dependencies haven be installed!
the result page is OK when I run the appplicaion, but the console will always say:
'ReferenceError: process is not defined'
why will this happened ? I am under Win7 64bit.

I finally found where the issue originates. It's not Jade or Express, it's Uglify-JS which is a dependency of transformers which is a dependency of Jade which is often a dependency of express.
I have experienced this issue in WebStorm IDE by JetBrains on Windows 7 and Windows 8 (both 64-bit).
I already went ahead and fixed the issue in this pull request.
All I needed to do was include process in the node vm context object. After doing that I received a new error:
[ReferenceError: Buffer is not defined]
All I had to do was include Buffer in the vm context object as well and I no longer get those silly messages.
I still don't fully understand why it only happens during debugging but in my extremely limited experience I've come to find that the node vm module is a fickle thing, or at least the way some people use it is.
Edit: It's a bug in the node vm module itself. I figured out how to reproduce it and I figured out why it only happens during debugging.
This bug only happens if you include the third (file) argument to vm.runInContext(code, context, file);. All the documentation says about this argument is that it is optional and is only used in stack traces. Right off the bat you can now see why it only happens during debugging. However, when you pass in this argument some funny behavior begins to occur.
To reproduce the error (note that the file argument must be passed in or this error never occurs at all):
The file argument must end with ".js" and must contain at least one forward slash or double backslash. Since this argument is expected to be a file path it makes sense that the presence of these might trigger some other functionality.
The code you pass in (first argument) must not begin with a function. If it begins with a function then the error does not occur. So far it seems that beginning the code with anything but a function will generate the reference error. Don't ask me why this argument has any effect on whether or not the error shows up because I have no idea.
You can fix the error by including process in the context object that you pass to vm.createContext(contextObject);.
var context = vm.createContext({
console: console,
process: process
});
If your file path argument is well-formed (matches the requirements in #1) then including process in the context will get rid of the error message; that is, unless your file path does not point to an actual file in which case you see the following:
{ [Error: ENOENT, no such file or directory 'c:\suatils.js']
errno: 34,
code: 'ENOENT',
path: 'c:\\test.js',
syscall: 'open' }
Pointing it to an actual file will get rid of this error.
I'm going to fork the node repository and see if I can improve this function and the way it behaves, then maybe I'll submit a pull request. At the very least I will open a ticket for the node team.
Edit 2: I've determined that it is an issue with WebStorm specifically. When WebStorm starts the node process we get this issue. If you debug from the command line there is no problem.
Video: http://youtu.be/WkL9a-TVHNY?hd=1

Try this...
set the webstorm debugger to break on all unhandled exceptions. then run the app in debug mode. I think you will find the [referenceError] is being thrown from the referenced fs.js.
More specifically, fs.js line 684:
fs.statSync = function(path) {
nullCheck(path);
**return binding.stat(pathModule._makeLong(path));**
};
These were my findings using the same dev environment as you. (win 64, webstorm, node, etc...)
from there you can use webstorms evaluate expression to re-run that line of code and see exactly why you are failing.

I had the same error coming up and it was because I had the following at the top of my file:
const argv = require("minimist")(process.argv.slice(two));
const process = require("child_process");
That confused node, I guess it thought process hadn't been defined at that point.
Changing the second line to a different variable name resolved the issue

Related

Angular Build - Uncaught TypeError: Cannot read property 'id' of undefined

I have managed to build my angular app out as a dev build. I haven't done it as a production build yet as it gives me a few errors and i just need to test the dev build.
The dev build process goes fine, no errors or anything. I then use the files from the dist folder in a nginx docker container to host the files.
The problem is nothing is displayed but a white page and in the console i get an error saying 'Uncaught TypeError: Cannot read property 'id' of undefined'. The full message below doesn't seem to point to anything i have written and i've spent several hours searching online but can't find anything on this problem.
I've tried a few different things such as running 'npx ivy-ngcc' which i read manually compiles some stuff. Is there anyway i can get more details on the error to see if it's something i have done?
UPDATE
So i have restored the line that i commented out in main.ts as mentioned in the comments below. I have also tried 'ng build --aot' as suggested which presents me with a series of errors that all seem to relate to devextreme components that are used. I find this strange as i started the project with the devextreme angular starter project from github.
i get messages such as:
'dx-scroll-view is not a valid HTML element'
'node_modules/devextreme-angular/ui/drawer.d.ts - error: appears in
the NgModule.imports of SideNavOuterToolbarModule, but could not be resolved to an NgModule class'
If you go in the devtools and click on Sources, "Don't pause on exceptions" and check "Pause on caught expecptions" and continue until you get the "id error" you will find what module the error is thrown. In my case was a third party library called 'ngx-card/ngx-card' and it's module was the cause of the error (CardModule). Hope this will help find at least the cause of the error
I managed to solve the problem by disabling ivy in the angular compilation options. As soon as i did that it worked building both dev and production versions and is now working perfectly within Nginx.
Thanks to everyone who offered help :)
In tsconfig.json of your Angular project, put this to disable Ivy, the new Angular template engine
{
...
"angularCompilerOptions": {
"enableIvy": false
}
}
Typically, if it's not something that you've written, it tends to be an issue w/ your implementation - i.e. "Visiting a food vendor and ordering a food item they don't provide".
I know it's not a specific answer, but ensuring that you have appropriately configured things in your app.module would be a good first step. Perhaps attempting to build w/ AOT will also give you some more verbose failures that stem from attempting to build out.
Hopefully this helps another poor soul.
To anyone using devextreme, make sure you update your version to at least 19.2.5
https://github.com/DevExpress/devextreme-angular/issues/975#issuecomment-580172291
Starting with version 19.2.5 we support the IVY compiler.
I had the same issue and fixed it by changing from
loadChildren: './app/page/account/account.module#AccountModule'
to
loadChildren: () =>
import('./app/page/account/account.module').then(
(m) => m.AccountModule
)
in app-router.module.ts
The root cause of your error is very likely to be a module that you needed to load explicitly but didn't, or a circular reference in your own modules. Rodrigo has a good answer but to be more specific, you need to find the registerNgModuleType function in Angular's core.js and set a conditional breakpoint on the first line. The condition should be !ngModuleType || !ngModuleType.ɵmod. (You can set a conditional breakpoint in most modern browsers by right-clicking the line number.)
Once you've paused execution just before the exception happens, you can look at the value of ngModuleType if it's not undefined, or walk up a frame or two in the scope and see what the value of imports was.
For me, this issue occurred while using Storybook.
The reason it happened was because of the way I was precompiling the node modules. I was doing:
Incorrect
ngcc --properties es2015 browser module main --first-only
Correct
ngcc
Using this approach fixed it

Log statements in Node Module not getting printed

I am a new to Node JS. I have included a module via npm install '<module_name>. It was built correctly and there was no errors. Now, I wanted to debug it, so I placed a few console.log('some text') in code blocks of the module to see if the code by passes that line. Anyway, none of the log statements were displayed.
I am wondering if I have to compile or something the modules after adding the log staements. Am I missing something here.
Your console.log statements are not being run, this could be caused by many things.
Assuming you have added the console.log statements to the module code in the node_modules directory of your app..
does the module have src and dist directories and you have not edited the code that is actually being run? (this relates to needing to recompile, but editing the actual code that the module is running will be quicker and easier)
if this is in a server or long running script it will need to be restarted to load the changes
is this in a browser which might be caching the code (turn off browser cache)
is the code where you added the log statements actually being hit?
I would make sure I had a console.log statement in a part of the code guaranteed to be hit, just as a sanity check.
For anyone coming here in the future, try console.error instead of console.log. For some decided reason or another, log was being overriding by the library I was monkey fixing. Took me way too long to find the culprit.

Node.js - write CSV file creates empty file in production, while OK in Mocha testing

This gist shows a code snippet that dumps an object into a CSV file.
File writing is done using module csv-write-stream and it returns a promise.
This code works flawlessly in all the Mocha tests that I have made.
When the code is invoked by the main nodejs app (a server-side REPL application involving raw process.stdin and console.log invocations as interaction with the user), the CSV file is created, but it's always empty and no error/warning seems to be thrown.
I have debugged extensively the REPL code with node-debug and the Chrome dev tools: I am sure that the event handlers of the WriteStream are working properly: no 'error', all 'data' seems to be handled, 'close' runs, the promise resolves as expected.
Nevertheless, in the latter case the file is always 0 bytes. I have checked several Q&A's and cannot find anything as specific as this.
My questions are:
can I be missing some errors? how can I be sure to track all communications about the file write?
in which direction could I intensify my investigation? which other setup could help me isolate the problem?
since the problem may be due to the presence of process.stdin in the equation, what is a way to create a simple, light-weight interaction with the user without having to write a webapp?
I am working on Windows 7. Node 6.9.4, npm 3.5.3, csv-write-stream 2.0.0.
I managed to fix this issue in two ways, either by:
resolving the promise upon the 'finish' event of the FileWriteStream rather than on the 'end' event of the CSVWriteStream
removing the process.exit() I was using at the end of my operations with process.stdin (this implies that this tutorial page may be in need of some corrections)

How to track an error in node.js traceback?

I'm very new to Node.js and right now I'm looking at the traceback after an error. The traceback starts with something promising:
buffer.js:140
throw new TypeError('must start with number, buffer, array, or string')
The question is: how to find this buffer.js so I can start investigating?
Context and what I've done:
It's a custom app and I started it with babel-node index.js. I have little knowledge about babel or other such machinery.
I've searched the app's directory for buffer.js. I've found two files called buffer.js, but neither of them has this line 140 or anything resembling that.
From what I see babel-node is installed in my AppData/Roaming/npm. It's installed “globally”; again, I'm not quite sure what it means, maybe that it's on PATH. I've searched that directory as well and found (the same) two buffer.js files, neither of which is relevant.
My understanding is that this is a Node.js app, so I've searched the Node.js directory. Here I found two different buffer.js files from some promzard, but they're not what I'm after.
So how do I locate this file? Where else to look? Could it be that it's embedded into something and thus the name just a remnant?
Print the exception stack trace this will help you and it will show the complete path how error produced and at which line and file generating the error.
Write the following line in your function where you are catching the exception.
console.log(ex.stack);

Trying to launch my node.js program with npm but nothing happens

I am trying to make this slack bot run : https://github.com/lmammino/norrisbot
I am not very skilled with npm and node yet, but I follow his instructions and try to run the bot with the help of the npm start command.
Here's the output I get :
F:\norrisbot>npm start
> norrisbot#1.0.5 start F:\norrisbot
> node bin/bot.js
F:\norrisbot>
No error, but nothing happens either in the console or the slack general channel...
By the way I set up my BOT_API_KEY variable correctly (with the token.js method)
By your command prompt it's clear you're running in Windows. The operations for running Node properly in Windows are different in several ways from Mac/Linux, and a LOT (most?) of developers don't address these because they're on Mac/Linux themselves. Path formats, file locations, how you expose environment variables, and all sorts of things are different in Win.
Try hand-editing bin/bot.js in your locally cloned copy of the repo. Find this line at the end of the file:
norrisbot.run();
Change it to read as follows:
console.log('Running Norris Bot');
norrisbot.run();
console.log('Ran Norris Bot');
I bet you will find that either NEITHER of these lines gets printed, or only one does.
If NEITHER line gets printed, the issue is with the npm command improperly formatting the path to the executable script for Windows users. In that case, try running it as (make sure NodeJS is in your PATH):
node bin/bot.js
If only the FIRST line gets printed, there is almost certainly a bug elsewhere in the module itself. I didn't evaluate all of its code, and I'm not on Windows myself at the moment - I just use it often enough to be aware of its differences. But either way it will get you started on finding the issue, and if it's truly a bug, you can pursue the bug report I see you've already filed in Github.

Resources