Yeoman subgenerator cannot read property 'toString' of null - node.js

I am creating a Yeoman Generator, which up until this point has been working correctly. My index.js runs correctly in my app folder, generating the initial structure. In that file, I frequently use this.fs.copy() and this.fs.copyTpl(), and both work fine. Now, I am in the process of creating a subgenerator which extends yeoman.generators.NamedBase as is required from reading the documentation here.
My file structure appears to be correct according to the documentation on the same page I linked to above. It is like so:
├───package.json
├───app/
│ └───index.js
└───view/
└───index.js
└───templates/
└───views/
└───view.html
Here is the code I am attempting to use to generate this new "view" in my project, located in the index.js in the view directory.
'use strict';
var yeoman = require('yeoman-generator');
module.exports = yeoman.generators.NamedBase.extend({
copyFile: function() {
this.fs.copyTpl(
'views/view.html',
'app/views/'+this.name.toLowerCase()+'.html',
{ name: this.name.toLowerCase() }
);
}
});
I ran yo {generatorname}:view helloWorld expecting it to create a new file based on the view.html template, which is the following file, very simply:
<p>This is the <%= name %> view.</p>
When I ran that command, I got the following error.
events.js:85
throw er; // Unhandled 'error' event
^
TypeError: Cannot read property 'toString' of null
at copy.process (/Users/woodruffjb/dev/yeoman/{generatorname}/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy-tpl.js:11:33)
at applyProcessingFunc (/Users/woodruffjb/dev/yeoman/{generatorname}/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:12:16)
at EditionInterface.exports._copySingle (/Users/woodruffjb/dev/yeoman/{generatorname}/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:52:24)
at EditionInterface.exports.copy (/Users/woodruffjb/dev/yeoman/{generatorname}/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy.js:22:17)
at EditionInterface.module.exports [as copyTpl] (/Users/woodruffjb/dev/yeoman/{generatorname}/node_modules/yeoman-generator/node_modules/mem-fs-editor/actions/copy-tpl.js:9:8)
at module.exports.yeoman.generators.NamedBase.extend.copyFile (/Users/woodruffjb/dev/yeoman/{generatorname}/view/index.js:7:11)
at /Users/woodruffjb/dev/yeoman/{generatorname}/node_modules/yeoman-generator/lib/base.js:408:16
at processImmediate [as _immediateCallback] (timers.js:358:17)
I attempted to see if the issue was that this.name was undefined or null, but when I did a console.log(this.name) I got the correct response, which was helloWorld. After a lot of googling the only stuff I could find on that error is for other things completely unrelated to what I am attempting to do, including Atom packages and Node.js server code.
All I can think of is that I'm somehow using this.fs.copyTpl() incorrectly? However, I am using it exactly the same as I use it in my app/index.js file to do the initial project setup. After all my searching I am stuck with this issue. According to the documentation on that function located here I seem to be doing it right. Anybody know my error?

Chances are 'views/view.html' is not found on disk.
This is because every this.fs methods won't magically resolve your paths. The safe way is to always provide absolute path. In your case you want to copy the file located in your template directory, so you'll use this.templatePath('views/view.html') to get the absolute path to it.

Related

Electron NodeJS Ag-Grid Cannot Import

In render.js I have
import { Grid } from 'ag-grid-community';
import { tableMethods } from './table.js';
I created my own custom table methods and decided to try ag-grid since mine aren't as fast as theirs (no surprise). Unfortunately, while I could get my own methods loading from my js file by setting
<script type="module" src="render.js"></script>
I cannot get ag-grid to load, I get this error
Uncaught TypeError: Failed to resolve module specifier "ag-grid-community". Relative references must start with either "/", "./", or "../".
I used npm i ag-grid-community -D to install it. I wasn't sure if I needed the -D, so I tried without that and it still shows same error.
*Note - of course I've tried doing what the error message says. But it didn't resolve and the documentation doesn't mention anything about this.
I was able to get this working by adding following before my render.js file
<script src="ag-grid-community.js"></script>
I also disabled type=module once I realized this is probably the intended way to split up rendering scripts, or at least that was my guess.

React native crypto stream module is undefined

I'm giving a try with [react-native-crypto][1] in order to learn how to convert nodejs to be used in React Native project in the future. Unfortunately, I couldn't get it running successfully. I've faced an issue with stream is undefined. ERROR TypeError: undefined is not an object (evaluating '_$$_REQUIRE(_dependencyMap[0], "stream").Transform.call').
If you have ever faced a similar problem, I'm so grateful for your help.
Also, I attach the screenshot of the issue as the following
For anyone still trying to solve this issue, I have figured out a solution that worked for me. So within node_modules/cipher-base/index.js, the top of the file should have a line which defines the variable Transform as var Transform = require('stream').Transform. For some reason, it does not like the module stream and as such it needs to be changed to readable-stream. Therefore the variable Transform should now read var Transform = require('readable-stream').Transform.
From what I have gathered, the stream module it is trying to refer to isnt actually a module that can be used. The reason why it gets referenced however seems to be because the tsconfig.json file in the root directory specifies "stream": ["./node_modules/readable-stream"] as a path, almost as if to make stream refer to the readable-stream module, which in theory it should refer to when it is called. But in this case it doesnt happen so we need to explicitly define that we are refering to the readable-stream module.
Hope this helps anyone else out there and prevents others scratching their heads for hours on end like it did for me!
I have figured it out by editing in metro.config.js as the following:
resolver: {
extraNodeModules: {
stream: require.resolve('stream-browserify'),
}
},

How requiring a module on entry point makes available on other modules on NodeJS?

This is probably a duplicated question, but I couldn't find anything.
Since I'm learning NodeJS, I think that I'm not using the right words to search, so it's hard to find an answer.
Here is the situation:
I'm currently following an online course about NodeJS and coding an API.
In the current step we are using Winston library to log errors. The instructor, have configured on Index,js, which is the entry point of the app, like this:
File: index.js
const winston = require('winston');
const errorHandler = require(./middleware/error.js);
//(...) some other imports
app.use(errorHandler);
winston.add(winston.transports.File,{filename:'logFile.log'});
And in other module we've created in the course to handle errors, he requires winston and simply call to log the error. Something like this:
File: error.js
const winston = require('winston');
function errorHandler(err,req,res,next){
winston.error(err.message,err);
res.status(500).send("something failed");
}
module.exports = errorHandler;
After doing a test, the error is correctly written to the file, and my question is: How it works? How a setting made on the 'required version' of winston at index.js is visible from the other required version at error.js?
From index.js we are importing error.js too, so i can imagine somehow this two modules are sharing this winston object, but again, I don't understand how or where is it shared.
Again, please excuseme if I'm not using the right terms to refer anything here, I'll accept any advice.
Thanks.
When a module is loaded in node.js, it is cached by the require() sub-system. So, when you then require() it again, that means you'll get the exact same module as the previous one.
So ... if you initialized the module after you first loaded it and the module stores some state that represents that intialization, then subsequent use of that module will be using the same (already initialized) module.
And in other module we've created in the course to handle errors, he requires winston and simply call to log the error.
It gets the same instance of the winston module that was already initialized/configured previously.
After doing a test, the error is correctly written to the file, and my question is: How it works? How a setting made on the 'required version' of winston at index.js is visible from the other required version at error.js?
Module caching as describe above. There's only one winston module that all are sharing so if it's initialized/configured in one place, all will use that configuration.

How to list all the images/videos within a folder from cloudinary in Node.js?

I'm a newbie to Node.js. I've started building an app by cloning the cloudinary sample project from github. Then I changed my working directory to photo_album and installed all the dependencies required for the app. And then, I did some changes as per my requirements and made it image and video gallery app. 1. When I try to list all the images within a folder named my_photos something like cloudinary support
cloudinary.api.resources(function(result){console.log(result)}, { type: 'upload', prefix: 'my_photos/' }); Then, I'm getting this error
/home/fw66/WebstormProjects/cloudinary_npm/samples/photo_album/node_modules/cloudinary/lib/utils.js:1023
return callback(void 0, result);
^
TypeError: object is not a function
at /home/fw66/WebstormProjects/cloudinary_npm/samples/photo_album/node_modules/cloudinary/lib/utils.js:1023:18
at IncomingMessage. (/home/fw66/WebstormProjects/cloudinary_npm/samples/photo_album/node_modules/cloudinary/lib/api.js:103:51)
at IncomingMessage.emit (events.js:117:20)
at _stream_readable.js:944:16
at process._tickCallback (node.js:458:13)
Process finished with exit code 8 But when I try something like the following, Then, I'm getting the list of all the uploaded images irrespective of the specified folder.
cloudinary.api.resources().then(function(result){console.log(result)}, { type: 'upload', prefix: 'my_photos/' }); But I need only and only those images which are uploaded inside the folder mentioned above, not outside of it. So, How do I do that? 2. When I try to list all the videos within a folder named my_videos something like the following
cloudinary.api.resources(function(result){console.log(result)}, { resource_type: 'video', prefix: 'my_videos/' });
Then, I'm getting the same error as I did for images above. And when I try something like the following, Then, I'm getting the list of all the uploaded images instead of videos in the api response.
cloudinary.api.resources().then(function(result){console.log(result)}, { resource_type: 'video', prefix: 'my_videos/' }); I'm not sure what is wrong with my code. Please help me out here. Thank you!
I assume you're using the cloudinary is defined using require('cloudinary').v2 and in that case you should be using a bit different syntax.
Essentially, the callback function should come last and be receiving both error and result parameters. Also, note that once you provide a parameter hash, a type must be provided as well. For example:
cloudinary.api.resources({type:"upload",prefix:"my_photos/"}, function(error, result){console.log(error, result)})

what does var io = require('../..')(server) do?

I've build the project https://github.com/Automattic/socket.io/tree/master/examples/chat locally and it is working great. However, it would be nice to understand a little more about how a socket application works.
In the main startup script one of the modules that is pulled in with require is
var io = require('../..')(server)
what does require('../..') do?
thanks!
When a path to a directory is given to require, it will implicitly look for an index.js in that directory.
In this case, it's the equivalent of
var socket = require("../../index.js");
var io = socket(server);
In the example provided, they're just using some shorthand and throw away the intermediate value returned by the call to require.
Check out the module.require docs for more info.
Here, in your code
require('../..');
Will add File form the path, which have used SOCKET.IO, as you can see that you have not added Socket.io module.
Also, if no specific path give for file or folder, Module require will try to load index.js or index.node. if no such file exist then it will give error.

Resources