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

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

Related

Electron require node_modules in main process: Error: Cannot find module ‘linvodb3’

For a angular2/electron learning app I use LinvoDB for persistent data storage based on the angular2-electron-starter seed that comes with two package.json one in root/ one in root/electron. After successful packaging the app.exe throws this error:
A JavaScript error occurred in the main process
Uncaught Exception:
Error: Cannot find module 'linvodb3'
So far I tried:
installing linvodb3 in / and /electron (npm install linvodb3 --save)
installing linvodb3 with --build-from-source
using electron-rebuild in both directories
apm install
multiple deinstallations and
installations of various packages recommended at similar questions.
var LinvoDB = require('linvodb3'); in the according module and main.js
const LinvoDB = require('electron').remote.require('linvodb3');
var LinvoDB = require('linvodb3'); in index.html similar to the jQuery questions.
The only thing I read about and couldn't try was to set the NODE_PATH manually cause I couldn't find a file where it's specified.
As suggested by #JensHabegger the initial problem can be solved by copying the node_modules from the /electron subfolder to /dist, I do this by a script in the /package.json.
The deeper problem of the not found leveldown lib couldn't be fixed with electron-rebuilt but with a postinstall script in /electron/package.json:
"scripts": {
"start": "electron .",
"postinstall": "cd node_modules/leveldown && node-gyp rebuild --target=1.4.8 --arch=x64 --dist-url=https://atom.io/download/atom-shell"
},

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

A dotfile that will set the default node version on a project using nvm?

In ruby when using rbenv you can make a .ruby-version file and put it in the local directory. https://gist.github.com/fnichol/1912050
I'm looking for something similar to this using NVM?
Question:
Is there a property to set in package.json or a file to create that will set the default version of node of a project?
You can do this with a combination of NVM, dotfiles in your project directory, and a little tool called direnv which allows you to load in environment variables on a per-directory basis.
http://direnv.net/
Install NVM and direnv, and then cd to the directory you want to change Node versions in.
Add a file called .nvmrc in that directory, containing just the version number of the Node version you want to auto-switch to, e.g.,:
6.2.2
Then add an environment configuration file called .envrc to your directory, containing this script:
nvmrc=~/.nvm/nvm.sh
if [ -e $nvmrc ]; then
source $nvmrc
nvm use
fi
PATH_add node_modules/.bin
If you now cd out of this directory, and then cd back in, direnv will kick in and you should be asked to add the directory to your direnv whitelist by typing direnv allow . at the prompt. Once whitelisted, direnv will auto-run that script whenever you enter this directory, setting your Node version to the version number in .nvmrc.
As a bonus, it will also add the node_modules directory to your PATH, so you can execute binaries from those directories without prepending the node_modules path.
There is now some native support for this built into direnv's stdlib. It's documented in their wiki, but the source is just as easy to read, or type direnv stdlib to find it).
$ node -v
v8.0.0
$ cat .node-version
4.3.2
$ cat .envrc
use node
$ export NODE_VERSIONS=~/.nvm/versions/node
$ export NODE_VERSION_PREFIX=v
$ direnv allow
direnv: loading ~/.config/direnv/direnvrc
direnv: loading .envrc
direnv: using node
direnv: Successfully loaded NodeJS v4.3.2 (via .node-version), from prefix (~/.nvm/versions/node/v4.3.2)
direnv: export +CPATH +LD_LIBRARY_PATH +LIBRARY_PATH +PKG_CONFIG_PATH ~MANPATH ~PATH
$ node -v
v4.3.2
$ direnv deny
direnv: error .envrc is blocked. Run `direnv allow` to approve its content.
$ node -v
v8.0.0
If you want node_modules/.bin in your path, just add the line layout node to your .envrc (the source is also in direnv stdlib output).
Here's how I do it, generally similar to Ross Shannon's answer, with a few differences:
You can specify the Node version in just the package.json, without requiring an .nvmrc file
You can also specify the Node version directly in the .envrc, again without an .nvmrc file
I don't add node_modules/.bin to the PATH, but if that's your preferred behavior, just add PATH_add node_modules/.bin to the use_nvm function.
For me, supporting Node version selection from package.json rather than .nvmrc was important because I didn't want to worry about keeping two files in sync (especially on a project with multiple collaborators). That said, this solution still works with .nvmrc.
This solution requires direnv, nvm and optionally (if you want to be able to select the Node version from package.json) jq.
In ~/.config/direnv/direnvrc file, add the following:
# To use:
# 1) Node version specified in package.json, in .envrc add:
# use nvm package.json
# This requires that package.json contains something like
# "engines": {
# "node": ">=6.9.2"
# },
#
# 2) Node version specified in .envrc add:
# use nvm 6.9.2
#
# 3) Node version specified in .nvmrc, in .envrc add:
# use nvm
use_nvm() {
local node_version=$1
if [[ $node_version = "package.json" ]]; then
if has jq; then
node_version=$(jq --raw-output .engines.node package.json | tr -d "<=> ")
else
echo "Parsing package.json for node version to use with direnv requires jq"
fi
fi
nvm_sh=~/.nvm/nvm.sh
if [[ -e $nvm_sh ]]; then
source $nvm_sh
nvm use $node_version
fi
}
In your project directory, add an .envrc file that calls use nvm, e.g.:
use nvm package.json
However, I prefer something like the following for my .envrc:
if declare -Ff use_nvm >/dev/null; then
use nvm package.json
fi
for shared projects with a shared .envrc so that collaborators who don't have use nvm defined don't get an error.
Now, when you enter your project directory, your desired Node version will be automatically used (the first time, you'll be prompted to whitelist your changes to .envrc with direnv allow).
Note: This solution does not switch the folder version automatically.
Other answers were not that helpful to me, and hence, this is the solution I followed:
Create a .nvmrc file and specify the node version you expect your app to run on.
touch .nvmrc
Open the file and specify the version, say 13.3.0
Post you define a version in .nvmrc, you can alternatively also define the engines in the package.json file, which will ensure that the version meets the requirements, if it doesn't, running npm install will fail.
"engineStrict": true,
"engines": {
"node": "13.3.0"
}
Below is the error you'll encounter if it fails to match the node version.
Lastly, to ensure that it switches to the right node version, you can either run the following command post navigating to the directory.
nvm use
And this will switch to the desired version, or you can add it to one of the script command in package.json like:
"scripts": {
/*
* here, the second command (nodemon server.js) will change base on the dev
* server you are using and the path where you've server.js
*/
"dev:app:run": "nvm use; nodemon server.js"
},
The above will switch the node version before it starts the server.

Bitcoinjs browser compile creating empty file

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

NodeJS unsafe-perm not working on package.json

I'm trying to run a npm install command with a preinstall script at my package.json. I know it's antipattern but I need to run some scripts as root.
It's working fine by adding a .npmrc file containing unsafe-perm = true to my root directory. But it's not working by add a config property in my package.json file:
{
"name": "foo",
"version": "1.4.4",
"config": {
"unsafe-perm":true
},
"scripts" : {
"preinstall" : "npm install -g bower"
}
}
// It is not working
According with NPM config docs it's ok adding this property in my package file. I want to understand why it's not working.
When you add that property, you are adding it to the environment of your script with the prefix npm_config_package:
$ cat package.json
{
"config": { "unsafe-perm": true }
}
$ npm run env | grep perm
$ npm run env | grep perm
npm_package_config_unsafe_perm=true
npm_config_unsafe_perm=true
$ sudo npm run env | grep perm
npm_package_config_unsafe_perm=true
npm_config_unsafe_perm=
$
This is for security reasons, sort of. It would not be good for an arbitrary package from the npm registry to allow you to change npm's config settings (e.g., what if it set prefix to /etc and installed a file named passwd)
However you can still get around it by setting the environment variable in on your script line (this will not work on Windows):
$ cat package.json
{
"config": { "unsafe-perm": true },
"scripts": { "foo": "npm_config_unsafe_perm=true env" }
}
$ npm run foo | grep unsafe_perm
npm_config_unsafe_perm=true
npm_package_config_unsafe_perm=true
npm_lifecycle_script=npm_config_unsafe_perm=true env
npm_package_scripts_foo=npm_config_unsafe_perm=true env
$ sudo npm run foo | grep unsafe_perm
npm_config_unsafe_perm=true
npm_package_config_unsafe_perm=true
npm_lifecycle_script=npm_config_unsafe_perm=true env
npm_package_scripts_foo=npm_config_unsafe_perm=true env
$
This may be a bug in npm though, so I would recommend not relying on this behavior. Can you get away with using a different user than root?
Source: Tested with npm#2.6.1 on OSX. I am a support volunteer on the npm issue tracker, https://github.com/npm/npm/issues.
unsafe-perm
Default: false if running as root, true otherwise
Type: Boolean
Set to true to suppress the UID/GID switching when running package scripts. If set explicitly to false, then installing as a non-root user will fail.
see the https://docs.npmjs.com/misc/config#unsafe-perm

Categories

Resources