Node module dependency - javascript

I am new to Nodejs and javascript and not sure yet how to ask the right question.
My main file has a var express = require('express'); on top of it. I am also creating a module that I will be requiring in my main file. I know that module also require the express module. Do I have to import the express module into it or the main file will take care of that?

Modules are completely self contained. Every module has to import the dependencies it needs.

Related

Using ES6 modules in Express

Is there a way to
write my code using ES6 modules in Express app;
without reverting to babel or #std/esm ?
once I am committed to app.js of Express, I can't find a way to get out of it.
This seems like something that should be already on the web, but all I can find is above options (transpiling, esm).
With node.js, you HAVE to tell it that your main file you are loading is an ESM module. There are a couple ways to do that. The simplest is to just give the main file a .mjs file extension.
// app.mjs
import express from 'express';
const app = express();
app.get("/", (req, res) => {
res.send("hello");
});
app.listen(80);
Then, start your program with:
node app.mjs
This works - I just ran it with node v14.4.0:. The others ways to do it are discussed in the link I previously gave you here. Per that documentation, there are three ways to specify you are loading an ESM module as the top-level module file:
Files ending in .mjs.
Files ending in .js when the nearest parent package.json file contains a top-level field "type" with a value of "module".
Strings passed in as an argument to --eval, or piped to node via STDIN, with the flag --input-type=module.

Unable to locate json module required in code (program runs fine and module is used)

one of the packages I am using happen to require a separate module that I want to look into:
var crypto = require('crypto');
however when I try to look up in /node_modules/ where the modules are, I cannot find the crypto module... I searched accross the project and the module does not show up...
it looks like crypto is part of the main modules - how do I look inside the core code? where to look?
The crypto module is part of the standard library -- https://nodejs.org/dist/latest-v4.x/docs/api/crypto.html

I'd like to reuse code from another module in my Nodejs backend

I'm using the Yeoman Generator Angular Fullstack and I'd like to reuse JS code from different directories within my server directory. I'm referencing the file that has the functions I want like:
var something = require('/.path');
I get the error: "Cannot find module" in my terminal.
I tried a number of variations of the path working through all of the levels from server level down to file level where the given functions are contained. I looked at a few tutorials:
http://www.sitepoint.com/understanding-module-exports-exports-node-js/
AND
https://www.launchacademy.com/codecabulary/learn-javascript/node/modules
I clearly missed something. Each module of my nodejs has a controller with an exports.create function. All of my code for each module is contained within my exports.create function accept for other required modules. I have no problem requiring underscore or other libraries in my Node/Bower modules by the way.
To be as detailed as can be, I expected
var something = require('./directory/directory.controller.js');
var something = require('/.path');
The path you are using is likely incorrect. Probably you want to open the file called path.js contained in the same folder of the file from which you are importing it. In order to do that, you should change the import as follows :
var something = require('./path');
./path is a relative path where . stands for current directory.
/.path is an absolute path. In this case require is importing a hidden file in the root directory. I guess is not what you want.

Should I pass package dependencies to a module function or just require them in the module file?

I understand that one can pass variables to a module by exporting modules as a function with parameters for each variable you wish to pass. E.g.
module.exports = module.exports = function (injectedVariable) {
app.get('/whatever', function (req, res, next) {
// Do something with injectedVariable
});
};
I understand that this is useful for passing variables like "app" around different modules. However, what is the preferred pattern for dealing with external package dependencies. I've seen code like this.
App.js
var jquery = require('jquery');
var _ = require('underscore');
var backbone = require('backbone');
var express = require('express');
// Pretty much every other external library
var app = express();
// Code here might config some of the 3rd party modules
require('routes/main.js')(app, _, jquery, backbone, someOtherModule, someOtherModule2);
require('routes/main.js')(app, _, jquery, backbone, someOtherModule, someOtherModule2);
// Etc
// Init server etc
And then each route module will export a function with all the paramaters available to support the packages it needs being passed to them.
Is there any reason for doing this? Would it not be cleaner to just require('whatever') at the top of each file? Presumably, having many require()'s for the same module across different files isn't a problem as node will then just pull out its cached version stored from the first instance of require() it encountered at runtime. Which method of managing package dependencies is preferred?
Is there any reason for doing this?
Using this pattern for regular modules like underscore, jquery, backbone, is blatant violation of the basic CommonJS pattern upon which node is built. Don't do it. Just require your modules directly using CommonJS syntax. For testing and dependency injection, there are CommonJS-compatible ways to do that such as rewire or injectr.
However, it's different with the express app instance as that is a specific object with a certain configuration, so it's OK to pass that in. However, express 4 eliminates the most common need to use this pattern since you can mount subapplications in an outer main application.

Access node.js File System module in Meteor

I'm creating a web app that will edit some config files stored on a user's HD, and decided to give Meteor a shot.
I'd like to use Node.js's File System module to handle to I/O of the config files, but I haven't been able to figure out how to include the module. After some searching, I found the following code here on StackOverlow, which is supposed to allow me to require the module:
var require = __meteor_bootstrap__.require;
var fs = require('fs');
However, even with this placed inside of the if(server) portion of my code, my application is still throwing an error and telling me that 'fs' is undefined.
Has anyone else encountered this issue?
From 0.6.0 you need to use Npm.require
var fs = Npm.require('fs');

Categories

Resources