Avoid ../../../ require in nodejs [duplicate] - node.js

This question already has answers here:
How can I make Node.js 'require' absolute? (instead of relative)
(39 answers)
Closed 8 years ago.
I'm creating an express 4 project and many of my files are nested within folders. Unfortunately I using a lot of :
var x = require('../../../../file');
I'm thinking I can avoid this if I have access to the base url of the project, but I'm seeing that using a global variable isn't a good idea. What's the best way to tackle this?

If you want directory of the root script from which the node process started, you can get it like this:
var root = require.main.filename.slice(0,require.main.filename.lastIndexOf('/'))
or, as #ChiChou suggested:
var root = require('path').dirname(require.main.filename)
This assumes that the main script (or any other script that requires your code) is run from the root directory.
You can use this root as your "base url".

Related

Rename files in sync from node js [duplicate]

This question already has answers here:
Renaming files using node.js
(5 answers)
Closed 3 years ago.
I have some files that I need to copy in another folder and then renaming all of them one by one. Is there any solution to rename all those files in the new folder one by one in synchronous way?
The function you're looking for is
fs.renameSync(old_file_path, new_file_path)
You can find it in the node documentation here
Remember you'll need to require in filesystem with const fs = require('fs') as well.

How do I read from a file located inside a Chrome extension bundle? [duplicate]

This question already has answers here:
How to access internal resources from background.js
(3 answers)
Closed 6 years ago.
Every page on the internet seems to suggest a different option for this, and who knows what's deprecated and what isn't. What's the "correct" way to read a file from within the extension bundle? I don't want to inline the data into the script files themselves, both because it's awful practice and because it consumes an unnecessary amount of memory.
Fetch is the newest and easiest. It works in all cases except getting a list of the packaged files at runtime, which requires chrome.runtime.getPackageDirectoryEntry.
fetch("manifest.json").then(function(response) {
return(response.json())
}).then(function(manifest) {
console.log(manifest)
})
https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

Nodejs chproc as simple user and not root

Hi this may be a simple question but i can't figure out how could I, using chproc.exec open an application, but my current program is running as root and I need the childprocess to run as a user.
How Could I achieve that?
Current code :
chproc.exec("google-chrome").unref();

node.js express - require file in all files

Let say I need to do
require('./config/globals.js');
in many files, what is the best way to do it? Writing this line in every file or there is some more elegant way?
Searching "node.js how to require in many files" returns answers to "node.js require all files in a folder" :(
If you have some global variables for your application that consists of multiple files and you want them to be accessible via all of the files in your application, define them as global variables in your main .js file:
server.js
global.myName = 'Carl';
require('app1.js');
require('app2.js');
In that scenario, both app1.js and app2.js will be able to read and write to the variabme myName.
Change the variables defined in your globals.js to follow the structure above, and it should achieve your goals - assuming I understood the question correctly.
EDIT: As seanhodges suggested, you could also keep the globals.js and edit accordingly:
Server.js
require('globals.js)
require('app1.js');
require('app2.js');
globals.js
global.myName = 'Carl';

Nodejs Code Reusing Best Practices

I am new to nodejs. I can't get my mind over organizing module code reusing in Nodejs. For example :
Let's say I have 3 files, which corresponds to 3 library files I wish to load. Then, I have 5 files which requires the 3 libraries.
Will I have to repeat typing the following in the 5 files?
require("./library-1.js");
require("./library-2.js");
require("./library-3.js");
Is there any way for me to automatically include these 3 lines of code (which is potentially more than just 3) in the 5 files?
Generally yes you end up with this kind of repetition, but the explicit dependencies are really helpful next year when you go to refactor your app. However, You can very easily wrap all 3 libraries into a monolithic module if you prefer:
//monolith.js
exports.lib1 = require('./library-1');
exports.lib2 = require('./library-2');
exports.lib3 = require('./library-3');
Then just load that with var monolith = require('./monolith');
Yes, you can require a folder as a module. If you want to require() a folder called ./test/.
Inside ./test/, create a package.json file with the name of the folder and a main javascript file with the same name, inside a ./lib/ directory.
{
"name" : "test",
"main" : "./lib/test.js"
}
Now you can use require('./test') to load ./test/lib/test.js.
similarly you can require other files

Resources