I'm trying to add gm module to my cloud. Since parse is not a node.js environment I made small changes over other modules I used, but gm module requires so much node core module. Do I have to push all of the sub modules to parse. Also how can I add the core modules. Changing require('xxx') to require('xxx/index.js') or require('xxx/xxx.js') failed.
These are the modules I could find where gm is depended and changed these files. I did include all the files in the modules only changed the following ones.
- gm/index.js
- events/events.js
- util/util.js
- stream/index.js
- emitter/index.js (also depends on util)
- asynclist/index.js
- eventproxy/index.js
changing all of these gives error
Result: Error: Module ./lib/eventproxy not found
at libs/eventproxy/index.js:1:18
at libs/asynclist/index.js:1:18
at libs/emitter/index.js:1:17
at libs/stream/index.js:22:15
at libs/gm/index.js:6:14
at main.js:118:14
// This error is caused by ./lib/eventproxy.
// It must be cloud/libs/asynclist/node_modules/eventproxy/lib/eventproxy.js
// Parse doesn't recognize './' as this folder in cloud code
where gm/index.js's require part is
var Stream = require('cloud/libs/stream/index.js').Stream;
var EventEmitter = require('cloud/libs/events/events.js').EventEmitter;
var util = require('cloud/libs/util/util.js');
My cloud folder structure is
cloud/libs/gm
cloud/libs/events
cloud/libs/util
cloud/libs/stream
cloud/libs/emitter
cloud/libs/asynclist
cloud/libs/eventproxy
EDIT: I found more dependencies. According to this, gm has 36 dependent libraries and I clearly need a simple solution.
EDIT: for the relative path problem with parse, this is a solution but as I said I need a simple way for the whole problem.
Related
I'm working with Node.js and Puppeteer, I'm using Puppeteer to scrape multiple websites. I'm thinking of organizing the code by separating each site implementation to its own file. So I will have like example.com.js example2.com.js etc
each of this will will require the same module(puppeteer/jsdom) and will export an object that contains variables and functions. This way, I can import all of these modules in the index.js file.
I've read about Nodejs caching modules and that this way of organizing of code is standard, however, in my case, if say I have 100 different website that I want to scrape, so I will have 100 module that will require (puppeteer/jsdom) in each one of them, so if Nodejs is caching the modules, I'm still using 100 different variables to store the modules.
const puppeteer = require('puppeteer');
So I'll have this line of code in 100 files, even if Nodejs is caching Puppeteer module, I'm still creating 100 puppeteer variable where I could have created just one if I wrote all code in a single file.
Am I missing something here ?
From the official document here
Modules are cached after the first time they are loaded. This means (among other things) that every call to require('foo') will get exactly the same object returned, if it would resolve to the same file.
You're correct about "Nodejs is caching Puppeteer module", but "I'm still creating 100 puppeteer variable" isn't true. So, I would say, don't worry :), you can continue to organize your code in multiple files.
For example, if this is your setup:
// a.js
const puppeteer = require('puppeteer');
// b.js
const puppeteer = require('puppeteer');
// c.js
const a = require('./a.js');
const b = require('./b.js');
The following will happen;
File a.js is required
The module puppeteer is being parsed and referenced by the variable puppeteer inside the file.
File b.js is required
Also creates a reference to puppeteer, but the module has already been parsed and executed, so it is 'cached'
Doing this won't cause a performance issue, regarding the import of the same module multiple times.
Regarding your statement
I'm still creating 100 puppeteer variable
As mentioned above, you're only creating references, the contents won't be deeply cloned.
I have a Node client-side application with the latest ag-grid version.
I was using ag-grid-community without any issues with this require line
const {Grid} = require('ag-grid-community');
and this new
new Grid(agGridDiv, agGridOptions);
but if I change the require to
const {Grid} = require('ag-grid-enterprise');
the new fails with exception 'Grid is not a constructor'
How can I fix this? I have tried various changes such as new Grid.Grid etc but nothing seems to work.
For latest 23.1.1 version this page:
// ECMA 5 - using nodes require() method
const AgGrid = require('ag-grid-enterprise');
Another way to follow this guide, it all depends on which repository you download the dependencies from.
import {Grid, GridOptions} from '#ag-grid-community/core';
import {LicenseManager} from '#ag-grid-enterprise/core';
// or
const {Grid, GridOptions} = require('#ag-grid-community/core');
I used core and it worked for import.
For old version:
Grid, like everything else, needs to be imported from ag-grid-community.
1) ag-grid-enterprise is pure additive functionality for ag-grid-community.
2) You will use ag-grid-enterprise via the ag-grid-community api not explicit. Use ag-grid-enterprise for LicenseManager only.
Off-topic:
I would recommend starting with the old version, since the source code of the new version is minified and it will be more difficult for you to understand many nontrivial nuances.
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.
I wish to use the 'utils-merge' node module already present as a dependency of express 4.12.3. I have express installed in my server application.
I have tried:
var merge = require('express/utils-merge');
and
var merge = require('utils-merge');
But it throws error 'Cannot find module'.
It is possible to do it using:
var merge = require('express/node_modules/utils-merge');
However, the standard practice is to explicitly require all of your dependencies so that you can directly require them.
You have to use relative path to submodule after parent module name (see documentation):
var merge = require('express/node_modules/utils-merge');
I wrote a simple npm module to precompile my handlebars templates when using django compressor to do post-processing for some client side components and found that I need to ship the npm module with a few js files.
Currently I just assume no one is installing this with the global flag because I've "hard coded" the path to these dependencies in the npm module itself
example layout of my npm module
/
* /bin
* /lib/main.js
* /vendor/ember.js
Now inside main.js I want to use the ember.js file ... currently my hard coded approach looks like this
var emberjs = fs.readFileSync('node_modules/django-ember-precompile/vendor/ember.js', 'utf8');
Again -this only works because I assume you install it local but I'd like to think node.js has a more legit way to get locally embedded files
Anyone know how I can improve this to be more "global" friendly?
What you can do is get the directory of the current file and make your file paths relative to that.
var path = require('path')
, fs = require('fs');
var vendor = path.join(path.dirname(fs.realpathSync(__filename)), '../vendor');
var emberjs = fs.readFileSync(vendor + '/ember.js', 'utf8');
Hope that helps!
One of the great strengths of Node.js is how quickly you can get up and running. The downside to this approach is that you are forced to fit the design patterns it was build around.
This is an example where your approach differs too much from Nodes approach.
Node expects everything in a module to be exposed from the modules exports, including templates.
Move the readFileSync into the django-ember-precompile module, then expose the returned value via a module export in lib/main.js.
Example:
package.json
{
"name": "django-ember-precompile",
"main": "lib/main.js"
}
lib/main.js
module.exports.ember = readFileSync('vendor/ember.js')
vendor/ember.js
You obtain your template via
var template = require('django-ember-precompile').ember
This example can be refined, but the core idea is the same.