crypto.createECDH is not getting added with webpack - node.js

I want to use createECDH function provided by crypto module in nodejs. I downloaded all the dependencies. Webpack does not add createECDH function in my resultant javascript files. How to use createECDH function of crypto available in node.
it adds number of functions like createCipher, createDechiper, createDiffihelman, listCiphers, getCiphers etc.

Possibly you're using old version of crypto lib. According to docs, ECDH class was added in v0.11.14, later than the rest of functions you mentioned.
It would be nice to see your code.

Related

What Modern Ways (If Any) Does Node Have to "Override" It's Import System?

In Node you can make an importText function fairly easily, and then use it in another file like so:
import importText from 'importText'
const css = await importText('./someFile.css')
// (This isn't technically possible yet, but top-level await is coming someday)
By using Webpack or a similar tool, you can also do the same thing, only with a more convenient syntax:
import css from './someFile.css';
Node used to have a way to do the same thing without Webpack, but the API for it, "require extensions", was deprecated (emphasis added):
Deprecated. In the past, this list has been used to load non-JavaScript modules into Node.js by compiling them on-demand. However, in practice, there are much better ways to do this, such as loading modules via some other Node.js program, or compiling them to JavaScript ahead of time.
https://nodejs.org/api/modules.html#modules_require_extensions
My question is: what are the "much better ways"?
They mention "compiling them to JavaScript ahead of time", which I would assume is the Webpack approach. But how would one "load a module via some other Node.js program" ... or solve the problem any other way (the text I quoted implied those two weren't the only options)?

How to use babel runtime with large nodejs/express app

I want to use babel runtime in a big/complex nodejs app. I don't want to use the babel require hook because the app is big and when I have tried to use it I get the following error:
RangeError: Maximum call stack size exceeded
And I only want to transpile a few JS files, at least for now.
The babel docs are a bit cryptic for the runtime support. After installing babel-runtime, they provide:
require("babel").transform("code", { optional: ["runtime"] });
Where does that code get included? And is "code" truly just a string? I have tried to add that to my main app.js file (express 3 app). Unfortunately, that doesn't work.
I cannot totally understand your questions, but I think I can answer part of it.
As explained in the babel api, transform() function takes a string that is supposed to be source code to be transpiled, and returns an object including three properties:
code the code generated
map the source map for the code
ast the syntax tree
This means, if you want to transpile your code in a folder, for each file you want to transpile, you should read the file with fs utility, give it to transform() function, and write the value of the code property in the object returned, to your output folder.
To simplify the step to read files, you could use the function transformFile provided by babel.
As for the problem you mention with your express app, I cannot help, unless you provide more information.

How to annotate core Node.js modules with JSDoc to have code hinting in IntelliJ?

So far I have a need for two of them, util and fs. Given a code like this:
var fs = require('fs');
var contents = fs.readFileSync('path/to/file', 'utf8');
Not only IntelliJ marks readFileSync as Unresolved function or method, but I am also devoid of, in my opinion, on of the best function given by IDEs - code hinting.
How can I annotate core Node.js functions with JSDoc to have code hinting and not have correct code erroneously marked as invalid?
Why core libraries are not resolved?
The issue with the invalid code is that you have "not NodeJS project".
You should install the NodeJS plugin: http://plugins.jetbrains.com/plugin/6098?pr=phpStorm
And then create a new project with the given sources.
IntelliJ will automatically overwrite the metadata and hook the NodeJS environment and all your core modules will be resolved.
Why there is no code completion?
IntelliJ tries hard do determine the object signature, but some JS coding techniques prevent your IDE from reading the signature (it can be read only in runtime).
Regarding the type annotation you can have a look here, for example: https://groups.google.com/forum/#!topic/scripted-dev/xFtPiBJDO4c
Update: I have written an article describing how you can enable the type hinting for NodeJS in IntelliJ IDEA: http://www.fse.guru/nodejs-autocomplete-in-idea-and-webstorm

bower init - difference between amd, es6, globals and node

I am creating my first Bower component. After running bower init the script asks me 'what types of modules does this package expose?' with these options:
amd
es6
globals
node
what is the difference between these options?
If you don't know, it's quite likely globals is the right answer for you.
Either way, you need to understand:
what is and why AMD
what is a nodejs module
what is ecmascript 6 and especially es6 modules
[UPDATE]
This feature was introduced very recently in bower and is not documented at all yet (AFAIK). It essentially describes the moduleType, which states for what module technology the package is meant to be consumed (see above).
Right now, It doesn't have any effect apart from setting the moduleType property in the bower.json file of the package.
See https://github.com/bower/bower/pull/934 for the original pull-request.
[UPDATE #2]
A few additional points, to answer comments:
right now AFAIK there is no validation done on the moduleType property - which means that people are technically allowed to use whatever value they want for it, including angularjs if they feel inclined to do so
the bower committee seems to not be keen toward the inclusion of additional non-interoperable/proprietary moduleTypes (think composer, angular, etc) - which is easily understandable, but yet again, nothing really prevents people from using the moduleType value they want
an exception to the previous is the (somewhat) recent inclusion of the yui moduleType, so, there are "exceptions" to be made, assuming they are part of a concerted plan
What I would do if I were to author a package for a not-listed package manager and publish it on bower?
I would author an es6 module, and use / patch es6-transpiler to output the package format I need. Then I would either/and:
petition the bower guys to include my package technology as a choice (based on the fact it's supported by es6-transpiler as a target)
publish my package including both the es6 module version of it and the transpiled XXX version of it, and use es6 as a moduleType
Disclaimer: I don't have real-life experience authoring angularjs modules.
Initial
I'm using bower init for first time too.
The options should refer to the different ways to modularize some JavaScript code:
amd: using AMD define, like requirejs.
node: using Node.js require.
globals: using JavaScript module pattern to expose a global variable (like window.JQuery).
es6: using upcoming EcmaScript6 module feature.
In my case I wrote a Node.js module dflow but I'm using browserify to create a dist/dflow.js file that exports a global dflow var: so I selected globals.
Other Updates
The command I used to browserify dflow as a window global object was
browserify -s dflow -e index.js -o dist/dflow.js
I changed it cause I prefer to use require also inside the browser, so now I am using
browserify -r ./index.js:dflow -o dist/dflow.js
and so I changed the bower.moduleType to node in my bower.json file.
The main motivation was that if my module name has a dash, for example my project flow-view, I need to camelize the global name in flowView.
This new approach has two other benefits:
Node and browser interface are the same. Using require on both client side and server side, let me write only once the code examples, and reuse them easily on both contexts.
I use npm scripts and so, I can take advantage of ${npm_package_name} variable and write once the script I use to browserify.
This is another topic, but, it is really worth that you consider how it is useful the latter benefit: let me share the npm.scripts.browserify attribute I use in my package.json
"browserify": "browserify -r ./index.js:${npm_package_name} -o dist/${npm_package_name}.js"
Just for reference, this is precisely what bower specifies regarding the module types:
The type of module defined in the main JavaScript file. Can be one or an array of the following strings:
globals: JavaScript module that adds to global namespace, using window.namespace or this.namespace syntax
amd: JavaScript module compatible with AMD, like RequireJS, using define() syntax
node: JavaScript module compatible with node and CommonJS using module.exports syntax
es6: JavaScript module compatible with ECMAScript 6 modules, using export and import syntax
yui: JavaScript module compatible with YUI Modules, using YUI.add() syntax
Relevant link: https://github.com/bower/spec/blob/master/json.md#moduletype

How to create a TypeScript Nodejs module spanning multiple files

I'm trying to create a Node.js module with a single namespace using TypeScript. I need the classes of the module to be in separate files, and have references to each other. I also need this module to utilize other node modules. But if I understand right, TypeScript only supports namespaces if a namespace is contained within a single file or the namespace does not use external modules.
Some people present the use of post-build tools to make the final module work, which is nice but doesn't address all the errors TypeScript throws when combining cross-file namespaces and imports during development.
Is it true that the closest solution is to create a module per file, and create a web of inter-imports?
I find that what works best for me for module dependencies is to always use source references, e.g.:
/// <reference path="other.ts" />
Since you can't generate single-file output from tsc if you use language-level imports/modules, I have resorted to using require as a function and so on.
This seems to be the same solution chosen by the TypeScript developers themselves (search for "require(" there). As suggested by silentorb's comment, you can declare the require function as some variation of the following (or use DefinitelyTyped):
declare function require(name: string): any;
Note that there's often a lot of discussion on the whole modularity topic in TS over on the TS forums (one recent example).

Resources