Bitcoinjs browser compile creating empty file - javascript

I'm attempting to do a build of Bitcoinjs for browser testing folloing the instructions on the BitcoinJS page (included below).
$ npm install -g bitcoinjs-lib
$ npm -g install bitcoinjs-lib browserify uglify-js
$ browserify -r bitcoinjs-lib -s Bitcoin | uglifyjs > bitcoinjs.min.js
When I run this is does generate a file called bitcoinjs.min.js but it is empty. Can anyone explain what I'm doing wrong?

what does your index.js look like?
try the following: Create index.js with the following content in the same folder:
var bitcoin = {
base58: require('bs58'),
bitcoin: require('bitcoinjs-lib'),
ecurve: require('ecurve'),
BigInteger: require('bigi'),
Buffer: require('buffer'),
elliptic: require('elliptic'),
bs58check: require('bs58check'),
}
module.exports = bitcoin;
Then run:
browserify index.js -s bitcoin | uglifyjs > bitcoinjs.min.js

Related

How to install/use a cypress plugin without using npm install

I'm trying to install/use this cypress plugin https://github.com/bjowes/cypress-ntlm-auth for my automation tests so I can login to an application that uses ntlm authenticator, but I cannot use npm install --save-dev cypress-ntlm-auth command cause of corporate security policies.
I've downloaded the zip repo release of this plugin and also have cypress installed, but I don't know the exact steps to do this without npm install.
I've tried adding this in the cypress/plugins/index.js file:
const ntlmAuth = require('cypress-ntlm-auth-3.2.5/test/e2e/cypress/plugins/index.ts');
module.exports = (on, config) => {
config = ntlmAuth.initNtlmAuth(config);
return config;
}
and also added this is cypress/support/index.js file:
import "cypress-ntlm-auth-master/src/commands"
but I have the following error while trying to open cypress: Error: Cannot find module 'cypress-ntlm-auth-3.2.5/test/e2e/cypress/plugins/index.ts' (the index.ts file is in the mentioned location)
I think I might be missing some installing/configuration steps. Can someone help?
Assuming you unzipped to node_modules, this is the difference between an npm install (left) and a zip (right).
Try hacking this
rename the unzipped src to dist
add http-mitm-proxy
#!/bin/sh
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
case 'uname' in
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
esac
if [ -x "$basedir/node" ]; then
"$basedir/node" "$basedir/../../../http-mitm-proxy/bin/mitm-proxy.js" "$#"
ret=$?
else
node "$basedir/../../../http-mitm-proxy/bin/mitm-proxy.js" "$#"
ret=$?
fi
exit $ret
add http-mitm-proxy.cmd
#IF EXIST "%~dp0\node.exe" (
"%~dp0\node.exe" "%~dp0\..\..\..\http-mitm-proxy\bin\mitm-proxy.js" %*
) ELSE (
#SETLOCAL
#SET PATHEXT=%PATHEXT:;.JS;=;%
node "%~dp0\..\..\..\http-mitm-proxy\bin\mitm-proxy.js" %*
)
These two files are also found in /node_modules/.bin with the file names cypress-ntlm and cypress-ntlm.cmd, which may be the critical bit, so copy them there as well.
I think using yarn might help : yarn add cypress-ntlm-auth
Install yarn by : npm install yarn -g

using and consuming typescript npm modules

So I want to write shared code in typescript, make an npm package of it and be able to install it..
So I tried writing an external lib like this :
export class Lib {
constructor(){
}
getData(){
console.log('getting data from lib');
}
}
Running npm link on external lib
And running npm link "package" in consuming apps directory, to have it appear in it's node_modules
consuming-app
index.ts
node_modules/
external-package-with-typescript
index.ts is simple it just looks like :
import { Lib } from 'external-package-with-typescript'
var l = new Lib();
Then I try to bundle it all with
browser index.ts -p tsify --global --debug > bundle.js
This results in an error from the external package
'import' and 'export' may appear only with 'sourceType: module'
I can get it to work with https://github.com/basarat/ts-npm-module-consume
But that produces an output that looks like
consuming-app/ index.ts
external-package-with-typescript/ index.ts
Which is of no value to me as I want one .js file that I can include in index.html
Ideas anyone?
Have you tried using declare instead of export?

using a module exported from browserify

I have a simple node.js project where I ran the following commands:
npm init
npm install buffer --save
browserify -r buffer -o buffer.js
How can I require that buffer.js file into other files or projects, and actually use the Buffer classed contained within it?
I have tried
var Buffer = require('./buffer.js');
var x = new Buffer();
but I get TypeError: Buffer is not a constructor
What am I doing wrong to be able to use that node module from another location?
Generate a UMD bundle with the -s flag:
browserify -r buffer -o buffer.js -s buffer
And then fix the file in which you require it:
var Buffer = require('./buffer').Buffer;
var x = new Buffer('some content');

How to manually bundle two javascript files?

I'm writing code in Javascript for Espruino (an embedded platform) and there is a restriction: Only one file is allowed to be saved into the device.
For the following code to work:
var x = new require("Something");
x.hello()
Something module should be reachable from main.js file. The Something module is a separate file and looks like this:
function Something() {
....
}
Something.prototype.hello = function() {
console.log("Hello there!");
}
exports = Something;
Question is; How can I concatenate these two files and still use the Something module as require("Something"); format?
The answer is found in Espruino forums:
# create bundle
BUNDLE="init.min.js"
echo "" > ${BUNDLE}
# Register modules via `Modules.addCached()` method
# see: http://forum.espruino.com/comments/12899741/
MODULE_NAME="FlashEEPROM"
MODULE_STR=$(uglifyjs -c -m -- ${MODULE_NAME}.js | sed 's/\"/\\\"/g' | sed "s/\n//g")
echo "Modules.addCached(\"${MODULE_NAME}\", \"${MODULE_STR}\");" >> ${BUNDLE}
uglifyjs -c -m -- init.js >> ${BUNDLE}
Why don't you use webpack to bundle the modules files into one file.
I suppose that your current directory is your project directory
First install nodejs at:
https://nodejs.org/en/download/
or sudo apt-get install nodejs
Second, install webpack using this command
npm i -g webpack
bundle your module
To build your bundle run this command:
webpack ./main.js bundle.js
Note: your bundle.js will include all the module files that you have required in main.js
There is https://www.npmjs.com/package/espruino tool that has function to prepare code (concatenate + minimize) for uploading to your board.
I use it in my build proc that compiles typescript code to javascript and then prepares for board upload. Tutorial is here https://github.com/espruino/EspruinoDocs/blob/master/tutorials/Typescript%20and%20Visual%20Studio%20Code%20IDE.md
Yes like it's mentioned above, you need to uglify the code. You cannot use webpack or grunt here. They will transpile the code which will break your require libraries.
Here is how I achieved this in my case:
I put all my files inside a folder (like /src). Don't import one file from another. Then minify all files in the folder into a single file (eg. index.js)
uglifyjs src/* --compress --mangle -o ./index.js
Then just upload this index.js into espruino. You need to install this library globally first.
npm install -g espruino
espruino -p /dev/cu.wchusbserial1420 -b 115200 --board ESP8266_4MB.json -e 'save()' index.js
I made a simple example here:
https://github.com/aliustaoglu/espruino-http-server

bundle a large node.js application into a single .js file

I would like to bundle a largish node.js cli application into a single .js file.
My code is structured as follows:
|- main.js
|--/lib
|----| <bunch of js files>
|--/util
|----| <bunch of js files>
...etc
I can use browserify to bundle the whole thing into one file using main.js as the entry point, but Browserify assumes the runtime environment is a browser and substitutes its own libraries (e.g. browserify-http for http). So I'm looking for a browserify-for-node command
I tried running
$ browserify -r ./main.js:start --no-builtins --no-browser-field > myapp.js
$ echo "require('start') >> myapp.js
but I'm getting a bunch of errors when I try to run $ node myapp.js.
The idea is that the entire application with all dependencies except the core node dependencies is now in a single source file and can be run using
$ node myapp.js
Update
=============
JMM's answer below works but only on my machine. The bundling still does not capture all dependencies, so when I try to run the file on another machine, I get dependency errors like
ubuntu#ip-172-31-42-188:~$ node myapp.js
fs.js:502
return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);
^
Error: ENOENT, no such file or directory '/Users/ruchir/dev/xo/client/node_modules/request/node_modules/form-data/node_modules/mime/types/mime.types'
You can use pkg by Zeit and follow the below steps to do so:
npm i pkg -g
Then in your NodeJS project, in package JSON include the following:
"pkg": {
"scripts": "build/**/*.js",
"assets": "views/**/*"
}
"main": "server.js"
Inside main parameter write the name of the file to be used as the entry point for the package.
After that run the below command in the terminal of the NodeJS project
pkg server.js --target=node12-linux-x64
Or you can remove target parameter from above to build the package for Windows, Linux and Mac.
After the package has been generated you have to give permissions to write:
chmod 777 ./server-linux
And then you can run it in your terminal by
./server-linux
This method will give you can executable file instead of a single .js file
Check out the --node option, and the other more granular options it incorporates.

Categories

Resources