Nodejs is confilicting with Requirejs when using require - node.js

I am new to requirejs I want to get rid of the conflict when calling require('fs').
I have read on http://requirejs.org/docs/faq-advanced.html that I can use a config that will combine requirejs with mainjs into a new js file but I am not sure where to put that piece of code to make it work.
Thanks in advance.

If you want to use require.js with node you should follow the instructions for node.
Basically you have to npm i requirejs and then (directly from the docs):
var requirejs = require('requirejs');
var foo = requirejs('foo');
var bar = requirejs('bar');

Related

Is there a reason for using var instead of const in Gulp config files?

According to the answers to this question Const in JavaScript: when to use it and is it necessary? it would make sense to use const for gulpfile.js configuration, would it not?
But in every usage description of gulp-modules, I see var being used like for instance:
var gulp = require('gulp');
var argv = require('yargs').argv;
var autoprefixer = require('gulp-autoprefixer');
//...
As I understand it, these declarations never change while Gulp is running.
Is there a reason for using var? Is it something with node.js compiler? Or is this just a habit?
It's a necessary habit.
gulp is a module and can be used in multiple locations in a code file. But, if you overwrite it accidentally the whole code will crash.
It's better to depend on the computer or compiler. Instead of human accuracy.
If you use const , you can never overwrite the variable. That will avoid error.
I suppose you are not using babel with gulp. If you are using it with babel, it would be better to use const or let instead of var.
If you are not using babel, then var is the only way for you. Read this blog post for further information: https://travismaynard.com/writing/using-babel-with-gulp
Or even better, this https://markgoodyear.com/2015/06/using-es6-with-gulp/

how can VS code support Go to definition

I have two JavaScript files: config.js, app.js. In app.js I want to use function defined in config.js so I could use require().
config.js
module.exports = {
somefunc: somefunc
}
app.js
var config = require('./config')
But I don't want to input the './' every time so I add a myRequire.js file.
myRequire.js
global.myRequire = function (p){
return require('./' + p)
}
In that case I could use myRequire('config') next time instead of myRequire('./config'), which might looks more concise.
app.js
require("./myRequire")
var config = myRequire('config')
config.somefunc()
But I met a problem, that I cannot use F12(Go to Definition) in VS Code to find the somefunc function. So could someone tell me what should I do to make it work in this case?
You are introducing a lot more problem than what you are trying to achieve. What if you are requiring a file from different file path '../../here', './over/there'.
If you really want to require something without a path. You can create your own npm module so you can require it globally without paths OR you create a folder with index.js in it and you require all the things you need.

How can I independently use submodule of a module in nodejs

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');

Three on node.js with ColladaLoarder

I know now how use JsonLoader in three.js for node.js.
But I have see, in the folder examples (three\examples\js\loaders) of node module an other loader whose is ColladaLoader.
I have try to execute this loader, but he isn't in the core folder of module.
I obtain an error: "ColladaLoader is not a function"
I have try to make a require to this file, but I obtain an error even I make a require to three module : "Three is not defined"
How can I use this ColladaLoader in node.js?
Thank You
var fs = require('fs');
var THREE = require("three");
eval(fs.readFileSync("bower_components/three.js/examples/js/loaders/ColladaLoader.js")+"");
First you load three.js, ensure you set it to a var with the uppercase name "THREE". Then you load the ColladaLoader - feels quite dirty.
A more beautiful solution would encapsule ColladaLoader.js to a node module.
If "ColladaLoader is not a function", then you did not reference to the ColladaLoader.js file.
Make sure to add <script src="js/loaders/ColladaLoader.js"></script> (or where ever your src is located) in your <head> or <body> somewhere before calling the ColladaLoader() function.

How to reference local files in a npm module?

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.

Resources