The priority of require () 's parameter to determine the file-extension in nodejs - node.js

I am learning node.js
for example, I try
var server = require("./myserver.js");
and
var server = require("./myserver");
Both of these two casework.
what if I have another file with the same name?
e.g myserver.json, myserver.node
, will it always search .js at first?
From one of the answerers in my previous question,
he mentions
only load the .json file if you explicitly add the .json extension to the require-call. So if you leave the extension, it always loads the .js file.
will this rule also suit to .node file?

If the exact filename is not found, then Node.js will attempt to load the required filename with the added extensions: .js, .json, and finally .node. You can check node_js docs for detailed explanation. https://nodejs.org/api/modules.html#modules_file_modules

Yes, If you will not provide the extension of the file, Then, it will first look at the JS file, since JS is by default

Related

Editing the .env file using node.js

I have a check.env file, in which I have some variables like
SharedAccessKey=
I want to put a value in the check.env file from my node.js code. Articles on internet are there for updating at the running time of node.js, but my requirement is to change in the file and keep the file with changes made.
How can I accomplish that.
I got this link : How to change variables in the .env file dynamically in Laravel?
but it is in some other language, how can I do in node.js.
I was unable to find out the best solution so went with another solution of mine that I took.
I am using two files now both .env extensions and I am copying main .env file to another empty .env file (like check1.env to check2.env).
Any modifications I am making is in the second file (check2.env).
And I am using string replacement in the .env file, using fs.readLine() and getting the string and the data.replace(), this worked for me.
The reason to use two .env files is that even if I change in the second file, again by copying from the first file I will get same string search and will replace with a different value.
-- Please suggest if there is an any better approach. Thanks

How to include static files on Serverless Framework?

I'm creating a NodeJS service with serverless framework to validate a feed so I added a schema file (.json) to the service but I can’t make it work.
It seems to not be included in the package. Lambda doesn't find that file.
First I just run sls package and check the zip contents but the file is not present.
I also tried to include the file with:
package:
include:
- libs/schemas/schema.json
but still not works.
How can I make sure a static file is included in the package and can be read inside the lambda function?
It depends on how you are actually trying to load the file.
Are you loading it with fs.readFile or fs.readFileSync?
In that case, Serverless doesn't know that you're going to need it. If you add a hint for Serverless to include that file in the bundle then make sure that you know where it is relative to your current working directory or your __dirname.
Are you loading it with require()? (Do you know that you can use require() to load JSON files?) In that case, Serverless should know that you need it, but you still need to make sure that you got the path right.
If all else fails then you can always use a dirty hack.
For example if you have a file 'x.json' that contains:
{
"a": 1,
"b": 2
}
then you can change it to an x.js file that contains:
module.exports = {
"a": 1,
"b": 2
};
that you can load just like any other .js file, but it's a hack.
From what I've found you can do this in many ways:
As it is stated in another answer: if you are using webpack you need to use a webpack plugin to include files in the lambda zip file
If you are not using webpack, you can use serverless package commnad (include/exclude).
You can create a layer and reference it from the lambda (the file will be in /opt/<layer_name>. Take in consideration that as today (Nov20) I haven't found a way of doing this if you are using serverless.ts without publishing the layer first (lambda's layer property is an ARN string and requires the layer version).
If your worried about security you can use AWS Secrets as it is stated in this answer.
You can do what #rsp says and include it in your code.
For those struggling to find a solution in 2022: use package.patterns parameter. Example:
package:
patterns:
- libs/schemas/schema.json
- !libs/schemas/schema-prod.json
(! in front of the file path excludes specified pattern)
Documentation: https://www.serverless.com/framework/docs/providers/aws/guide/packaging

ChromeWorker File Not including in the extension package

Hi I am building a Mozilla Extension through CFX tool. I have used ChromeWorker in it. It is working fine while i am running : cfx run command. But while building up a package using cfx xpi the Chrome Worker file is not included in the xpi package.
I am using this to create the worker thread.
var tworker = new ChromeWorker("chrome://addons/content/t_worker.js");
my t_worker.js file is present in addons/lib
I have also put one chrome.manifest file in the package that contains :
content addons ./resources/addons/lib/
Please tell the possible reason for this problem and also how to fix it .
Try moving your file into the data folder then do:
const self = require('sdk/self');
var tworker = new ChromeWorker(self.data.url('t_worker.js')
im totally not sure of this syntax, i just typed off top of my head
I putted that worker file in the lib thats why its not working. I just changed the location of my file to data folder and made changes in my chrome.manifest file : content addons ./resources/addons/data(previously it was lib)/ . Its working fine Thanks to #Noitidart for the suggestion of putting it in data folder

Order of resolution with nodejs require module

Suppose I have all of foo.js, foo.coffee, and foo.jsonin the same directory, and I say require './foo' from another (coffeescript) file in that location, what rule governs which one will be loaded?
A short experiment (using require.resolve './foo') would seem to indicate that the javascript file wins over the other two.
Indeed, looking at require.extensions it looks like .js being mentioned there as the first item—but then, object attribute names are inherently unordered in javascript, right?, so any name added to that property could potentially re-order the entries—could that lead to another resolution order?
Just wondering, as i couldn't find any documentation. it does become relevant when you do (and maybe you shouldn't) coffee --compile route/to/directory.
.js is loaded first (this also means that it's better to use full name.json for your fixture instead of name as it could be shadowed by name.js)
From "modules" documentation:
If the exact filename is not found, then node will attempt to load the required filename with the added extension of .js, .json, and then .node.
Also, read name resolution algorithm in pseudo-code:
LOAD_AS_FILE(X)
1. If X is a file, load X as JavaScript text. STOP
2. If X.js is a file, load X.js as JavaScript text. STOP
3. If X.node is a file, load X.node as binary addon. STOP
After (1,2,3) extensions set in require.extensions are checked in the order they a set (for CoffeScript, require("coffe-script") installs .coffee handler).
The behavior in V8 is to iterate over named properties in the order they were originally assigned, so I would expect .js to always be first.
This post references that behavior

Using a Twig Extension

I am trying to use teh Twig i18n Extension.
As far as I can tell the file I need is here:
https://github.com/fabpot/Twig-extensions/blob/master/lib/Twig/Extensions/Extension/I18n.php
Now I am not quite sure where to save this file
I have Twig in a folder called includes/lib (/includes/lib/Twig). I see a folder Extension under Twig. Do I save it here?
After I save it, do I need to do a "require_once" to the file or will Twig_Autoloader do the job for me?
I am not using Symfony2
Thanks
Craig
Here is the complete answer that worked for me:
Copy the file in Twig-Verzeichnis (extract i18n.zip in Twig).
For the I18n extension it would be Twig/Extensions/Extension/I18n.php
Eventually add other files requred by I18n. You will see what these are by the error messages that come. I had to add "Twig/Extensions/Node/Trans.php" and "Twig/Extensions/TokenParser/Trans.php".
In your config file add the following:
// Set language to German
putenv('LC_ALL=de_DE');
setlocale(LC_ALL, 'de_DE');
// Specify location of translation tables
bindtextdomain("project_de_DE", "./locale");
// Choose domain
textdomain("projectl_de_DE");
Register the Twig Extension
$twig->addExtension(new Twig_Extensions_Extension_I18n());
Create the directory locale/de_DE/LC_MESSAGES
Create the PO file (the easisest is to have a sample file to start from)
Open the file in a normal text editor (be sure to use utf-8 encoding) and start translating
Open the PO-Datei with PoEdit (www.poedit.net/)
Save to locale/de_DE/LC_MESSAGES (a MO-Datei will be created).
Add the translation to the appropriate places in the Twig-Template with
{% trans 'Text in the original language' %}`
You need to register this extension with Twig:
$twig->addExtension(new Twig_Extensions_Extension_I18n());
If your installation is configured correctly, the autoloader should do the job of including the file. If not, you could include the file manually or make the installation with composer.
It seems the "proper" way to install these extensions without Composer is as follows:
Download a release from https://github.com/fabpot/Twig-extensions/releases
Copy the contents of the lib/ directory somewhere to your project
include the file .../Twig/Extensions/Autoloader.php
Register autoloader: Twig_Extensions_Autoloader::register();
Continue as explained in the doc: http://twig.sensiolabs.org/doc/extensions/i18n.html

Resources