chalk.bgHex is not a function - javascript

I'm using chalk version ^1.1.3
No idea why it's saying that the method is not recognized:
const chalk = require('chalk'),
_ = require('lodash'),
info = chalk.yellow,
green = chalk.green,
script = chalk.bgHex('#ebd2f8')
Error: chalk.bgHex is not a function
I also don't know how I'd append a text color on top of that background.

The commit that introduced hex/bgHex was committed just 2 hours ago. There hasn't been a new publish to the NPM repository with that commit.
If you want to use it, you can use the GitHub-hosted version of the module:
$ npm i chalk/chalk
However, do so with extreme caution. There's probably a reason why the developers haven't published a new version yet (i.e. it's not finished).

Related

Obscurify react code in production by automatically changing all the function and variable names

Are there any libs that help obscurify a react build for production?
Something like:
const MyComp = () = > {
const {propa, propb} = useMyfunc()
return(...)
}
to
const xyz = () = > {
const {yxz, zyx} = zzz()
return(...)
}
Is this library what you are looking for?
Have you ever try this package?
this is a very professional package for obfuscate js codes which convert your code:
const MyComp = () = > {
const {propa, propb} = useMyfunc()
return(...)
}
to this:
const MyComp=()=>{const {propa:_0xa95d6e,propb:_0xfaabf6}=useMyfunc();return _0xa95d6e+_0xfaabf6;};
The right choice would be Terser. it is availble along with webpack too (terser-webpack-plugin) for ES6+.
uglify-es is no longer maintained and uglify-js does not support ES6+.
You can refer to benchmarks for different packages from this article.
UglifyJS has options to mangle (obscurify) names:
Sample input:
const MyComp = function() {
const {propa, propb} = useMyfunc()
return(1)
}
Sample output:
const n=function(){const{propa:n,propb:o}=useMyfunc();return 1};
Try it yourself: https://www.uglifyjs.net/
useMyfunc cannot be mangled unless the function definition is included (otherwise the function call would fail.) Similarly, mangling top-level globals like MyComp may break anything that uses that component.
If you bundle all your React code into a single file before mangling, it should work because all the references will be mangled to correctly matching names.
React already uses a minifier like UglifyJS, so you may be able to just modify some configuration files. Note source maps will undermine any mangling, so they should be disabled. (I think React is more interested in the smaller JS files than obscuring code.)
Complementing Ashwin R's response...
As pointed out, Terser is a good choice for doing it.
React uses some form of UglifyJS to minify your code on build, but in order to obfuscate variables and function names I did the following:
Instructions:
1° - Install Terser
npm install terser
2° - Modify package.json:
"terser": "terser ./build/static/js/*.js -c -m --mangle-props regex=/_$/",
"build": "react-scripts build && npm run terser"
That seems to have worked for me; hopefully it does for you as well!
react-obfuscate
There is one npm repository that takes care of react code obfuscation. Below is the link for the same -
https://www.npmjs.com/package/react-obfuscate
Basic steps are written in the repo documentation itself.
Jscrambler
Another tool that I find interesting is Jscrambler.
https://blog.jscrambler.com/protecting-your-react-js-source-code-with-jscrambler/

React Native - Random ReferenceErrors (Can't find variable)

When running our app I'm seeing some ReferenceErrors. They seem to be on and off because there are builds where the app works fine and in the next build it crashes on a ReferenceError. For example:
ReferenceError: Can't find variable: interpolatedCircleColor
ReferenceError: Can't find variable: _careItem (this comes from a component I created)
ReferenceError: Can't find variable: state
ReferenceError: Can't find variable: style
Each build I get one of these errors. It never seems to be missing more than one variable. There are four components where the errors come from. Other screens/components work just fine. These components don't work together in any way. They all just display data passed on as a prop. The errors are now visible in both development and release builds. On development builds the errors are shown, when I fix them everything seems to be OK. But after a release build it needs different variables and the app shows a white screen.
I used to overcome this problem by defining the missing variables as an empty string in the component that needed it. But this is just a hack to get it to work (sometimes). But with some 'variables' it expected a function this is where I decided the problem needed to be fixed for good.
Example. I would pass these variables in the render function of the component:
const interpolatedCircleColor = '';
const interpolatedTranslateX = '';
const handlerAnimation = '';
const height = '';
const style = '';
const state = '';
const opacityStyle = '';
const substitutionStyle = '';
const containerStyles = '';
const tintColor = '';
I suspect the problem is with bundling the app as this is where I can find the missing variables.
To build the app in release mode I use the following process:
rm -rf $TMPDIR/react-* && watchman watch-del-all && yarn cache clean
cd android && ./gradlew clean && cd ..
react-native bundle --platform android --dev false --entry-file index.js --bundle-output android/app/src/main/assets/index.android.bundle --assets-dest android/app/src/main/res --sourcemap-output android/app/src/main/assets/index.android.bundle.map
npx react-native run-android --variant=release
This would then give me one of the errors I mentioned above. In one of the four components.
Additional information:
"react-native": "0.63.1",
"react": "16.13.1",
node v14.5.0
npm 6.14.5
Any help would be greatly appreciated!

Exclude folders from builds in Brocfile

Is there a way to exclude a folder from a build in a Brocfile (or any other place).
The use case is packaging, where I have an app made of sub-apps within pods. eg.
/app/modules/components
/app/modules/app1
/app/modules/app2
/app/modules/app3
I'd like to build them all when environment is set to 'development' or only eg. 'app1' when environment is 'app1'. Any suggestions?
I have tried different combinations of broccoli-file-remover, broccoli-funnel and broccoli-merge-trees to no avail.
var removeFile = require('broccoli-file-remover');
module.exports = removeFile(app.toTree(), {
paths: ['app/modules/pod1/', 'app/modules/pod2/']
});
Ah, so after actually thinking about this clearly, everything is actually working exactly as expected in my previous example.
I clearly wasn't paying enough attention. app.toTree() is far too late to perform this operation, as everything has already been built and concated.
Luckily, ember-cli does enable addons to modify the appropriate trees at various life cycle milestones.
See: https://github.com/ember-cli/ember-cli/blob/master/ADDON_HOOKS.md for more details on which hooks are currently available.
The hook that should do the trick is Addon.prototype.postprocessTree. Now we have two choices, we can build a standalone addon, via ember addon or we can create a light-weight in-repo addon via ember g in-repo-addon. Typically for these types of situations, I prefer in-repo-addons as they don't require a second project, but otherwise they are the same.
ember g in-repo-addon remove
we need to install broccoli-stew via npm install --save broccoli-stew
include it var stew = require('broccoli-stew');
add hook postprocessTree to the add-on
when the postprocessTree is for the type we care about, use broccoli-stew to remove the directories we no longer care care.
The resulting pull request: https://github.com/WooDzu/ember-exclude-pod/pull/1
Note: I noticed template wasn't one of the types available in postprocess, so I added it: https://github.com/ember-cli/ember-cli/pull/4263 (should be part of the next ember-cli release)
Note: we really do want an additional hook
Addon.prototype.preprocessTree, as to ignore the files before we
even build them. I have opened a related issue:
https://github.com/ember-cli/ember-cli/issues/4262
output of the above steps
var stew = require('broccoli-stew');
module.exports = {
name: 'remove',
isDevelopingAddon: function() {
return true;
},
postprocessTree: function(type, tree){
if (type === 'js' || type === 'template') {
return stew.rm(tree, '*/modules/pod{1,2}/**/*');
} else {
return tree;
}
}
};
I am pretty confident broccoli-stew's rm will handle this correctly.
https://github.com/stefanpenner/broccoli-stew/blob/master/lib/rm.js#L4-L40 there are even tests that test a very similar scenario: https://github.com/stefanpenner/broccoli-stew/blob/master/tests/rm-test.js#L48-L57
var stew = require('broccoli-stew');
module.exports = stew.rm(app.tree(), 'app/modules/{pod1,pod2}');
If this doesn't work, feel free to open an issue on broccoli-stew. Be sure to provide a running example though
This is really late, but I created a Broccoli plugin to do just this. It's available at https://www.npmjs.com/package/broccoli-rm.
(The trick is to detect whether an excluded path is a folder, and then use a glob match to make sure that none of the children of the folder get symlinked during copying.)
var rm = require('broccoli-rm');
var input = app.toTree();
module.exports = output = rm([input], {
paths: ['app/modules/pod1', 'app/modules/pod2']
});

ReferenceError: TextEncoder is not defined

I'm writing a simple addon in Firefox - 24, on Linux.
I get the error:
ReferenceError: TextEncoder is not defined
when I do: var encoder = new TextEncoder();
the function I'm using is:
function write_text(filename, text) {
var encoder = new TextEncoder();
var data = encoder.encode(text);
Task.spawn(function() {
let pfh = OS.File.open("/tmp/foo", {append: true});
yield pfh.write(text);
yield pfh.flush();
yield pfh.close();
});
}
if you are having this error while running node server
locate this file node_modules/whatwg-url/dist/encoding.js or .../lib/encoding.js
add this line at top const { TextEncoder, TextDecoder } = require("util");
In nodejs you can solve with util:
var util= require('util');
var encoder = new util.TextEncoder('utf-8');
If you experienced this because of using Mongodb via npm install mongodb then the simplest way is just to upgrade your Node Version. Needs to be higher than version 12; I used version 16 and it clearly fixed my problem
This issue occurs in node 10 or lower version only. To resolve this issue upgrade node version to 12 or higher and then rm -rf node_modules && npm i
Or If you don't want to upgrade node version, then,
Locate this file
node_modules/whatwg-url/dist/encoding.js // If dist folder
node_modules/whatwg-url/lib/encoding.js // If lib folder
And add this line in encoding.js file
const { TextEncoder, TextDecoder } = require("./utils"); // if utils file
const { TextEncoder, TextDecoder } = require("./util"); // if util file
Ah, you're using the SDK, I gather when re-reading the actual error of your other question.
You need to import TextEncoder explicitly from some other module, as SDK modules lack the class.
You need to yield OS.File.open.
append: is only supported in Firefox 27+
.flush() is only supported in Firefox 27+ (and a bad idea anyway). Use .writeAtomic if you need that.
You write: true to write to a file.
Here is a full, working example I tested in Firefox 25 (main.js)
const {Cu} = require("chrome");
// It is important to load TextEncoder like this using Cu.import()
// You cannot load it by just |Cu.import("resource://gre/modules/osfile.jsm");|
const {TextEncoder, OS} = Cu.import("resource://gre/modules/osfile.jsm", {});
const {Task} = Cu.import("resource://gre/modules/Task.jsm", {});
function write_text(filename, text) {
var encoder = new TextEncoder();
var data = encoder.encode(text);
filename = OS.Path.join(OS.Constants.Path.tmpDir, filename);
Task.spawn(function() {
let file = yield OS.File.open(filename, {write: true});
yield file.write(data);
yield file.close();
console.log("written to", filename);
}).then(null, function(e) console.error(e));
}
write_text("foo", "some text");
I was also getting this error so I solved it in this way,
in nodejs project go to the node_modules/whatwg-url/dist/encoding.js file in that add this line =>
const {TextDecoder, TextEncoder} = require("util");
And your problem is solved
A text encoder for Node.js can be found in the util module. You can access it like so:
const util = require('util');
const TextEncoder = new util.TextEncoder();
One of the roles of the TextEncoder is to convert a string of text into an array of bytes. You can achieve this like so:
const data = TextEncoder.encode(
JSON.stringify({ c: "Green" })
);
// Uint8Array [ 123, 34, 99, 34, 58, 34, 71, 114, 101, 101, 110, 34, 125 ]
The array returned is called a Uint8Array. It consists of integers in the range 0 to 255.
Note that TextEncoder only supports UTF-8 encoding.
For me upgrade Node.js version resolved this issue
Open your encoding.js folder in node_modules>whatwg-url>dist
And in place of:
"use strict";
const utf8Encoder = new TextEncoder();
const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
Write this code:
"use strict";
var util= require('util');
const utf8Encoder = new util.TextEncoder();
const utf8Decoder = new util.TextDecoder("utf-8", { ignoreBOM: true });
all you where missing is this small part by including utils.
The TextEncoder can be found in the sdk/io/buffer module:
let { TextEncoder, TextDecoder } = require('sdk/io/buffer')
If it's an error in node_modules/whatwg_url/dist/encoding.js folder then uninstall MongoDB by
npm uninstall mongodb
and reinstall it
npm install --save mongodb
I was also facing the same problem in my project but I fixed this issue by upgrading my node version from 10 to 12. May be this issue now a days can occurred due to lower version of node we are using in our project.
This looks like a node version error because I solved it by updating from 10 to 16 and after that, I installed dependencies and open a new terminal.
Update node to 14 or higher, I used Node Version Manager (NVM)
Delete node_modules directory, on linux:
rm -rf node_modules
Install dependencies with npm install
Close and open a new terminal
Run app with node or nodemon
Done!
This might help others.
I was getting the same error and I almost tried all the above solutions, but nothing works for me. Finally, I update the npm version and everything is fine.
When I installed the Next App the npm version was 6.14.4.
I update the version and all errors are fixed you don't need to change anything in the core files just update the version in my case recommended 8.11.0.
npm -v // Check the version
npm install -g npm#latest // Get the latest version
OR
npm install -g npm#8.11.0 // Get the Spacific version
Complete guid here
delete your 'node_modules' folder
rm -rf node_modules
then re-install dependencies
npm i
If you are using 'mongoose' greater than v6 you need at least Node.js v12
I was facing the same error, because of having to install old nodejs. This problem can be solved by installing the latest nodejs.
To update nodejs to nodejs to 14.x
sudo apt update
curl -sL https://deb.nodesource.com/setup_14.x | sudo bash -
sudo apt install -y nodejs
node -v
It's a node version problem.
Described by #yhojann-cl here
I have the same problem:
In /usr/bin/node I have 10.x, but 16.x is installed by nvm.
In my case, I had multiple node versions installed, and current project required a more recent node version. To fix the problem I did the following steps.
To check the current version of node running, in the terminal use the command
node --version
Output in the Terminal :
The following command will list the different node versions already installed in your system.
nvm ls
Output in the Terminal :
To switch to more recent version of the node ie, v16.17.0, use the following command in the terminal
nvm use v16.17.0
Output in the Terminal :
Now confirm the current version of the node by
node --version
Output in Terminal:
I encountered this when running automated tests with jest and rendering a component that included import { AgGridColumn, AgGridReact } from "ag-grid-react".
The solution is to mock out that function as follows:
jest.mock('ag-grid-react', () => ({
__esModule: true,
AgGridReact: jest.fn((x) => <div>{x.children}</div>),
AgGridColumn: jest.fn(() => <div />),
}));
I was getitng the same error after adding the mongodb, i resolve it by upgrading to node version
nvm use 16.16.0
TextEncoder is native function in javascript, check the version that suit the ability.
https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#browser_compatibility
Chrome version >=38, Edge version >=79, Firefox version >=18, Node verison >=11.0.0...

How do I load my script into the node.js REPL?

I have a script foo.js that contains some functions I want to play with in the REPL.
Is there a way to have node execute my script and then jump into a REPL with all the declared globals, like I can with python -i foo.py or ghci foo.hs?
There is still nothing built-in to provide the exact functionality you describe. However, an alternative to using require it to use the .load command within the REPL, like such:
.load foo.js
It loads the file in line by line just as if you had typed it in the REPL. Unlike require this pollutes the REPL history with the commands you loaded. However, it has the advantage of being repeatable because it is not cached like require.
Which is better for you will depend on your use case.
Edit: It has limited applicability because it does not work in strict mode, but three years later I have learned that if your script does not have 'use strict', you can use eval to load your script without polluting the REPL history:
var fs = require('fs');
eval(fs.readFileSync('foo.js').toString())
i always use this command
node -i -e "$(< yourScript.js)"
works exactly as in Python without any packages.
I made Vorpal.js, which handles this problem by turning your node add into an interactive CLI. It supports a REPL extension, which drops you into a REPL within the context of your running app.
var vorpal = require('vorpal')();
var repl = require('vorpal-repl');
vorpal
.delimiter('myapp>')
.use(repl)
.show()
.parse(process.argv);
Then you can run the app and it will drop into a REPL.
$ node myapp.js repl
myapp> repl:
Another way is to define those functions as global.
global.helloWorld = function() { console.log("Hello World"); }
Then preload the file in the REPL as:
node -r ./file.js
Then the function helloWorld can be accessed directly in the REPL.
Here's a bash function version of
George's answer:
noderepl() {
FILE_CONTENTS="$(< $1 )"
node -i -e "$FILE_CONTENTS"
}
If you put this in your ~/.bash_profile you can use it like an alias, i.e.:
noderepl foo.js
I created replpad since I got tired of reloading the script repeatedly.
Simply install it via: npm install -g replpad
Then use it by running: replpad
If you want it to watch all files in the current and all subdirectories and pipe them into the repl when they change do: replpad .
Check out the videos on the site to get a better idea of how it works and learn about some other nice features that it has like these:
access core module docs in the repl via the dox() function that is added to every core function, i.e. fs.readdir.dox()
access user module readmes in the repl via the dox() function that is added to every module installed via npm,
i.e. marked.dox()
access function's highlighted source code, info on where function was defined (file, linenumber) and function
comments and/or jsdocs where possible via the src property that is added to every function, i.e. express.logger.src
scriptie-talkie support (see .talk command)
adds commands and keyboard shortcuts
vim key bindings
key map support
parens matching via match token plugin
appends code entered in repl back to file via keyboard shortcut or .append command
See: https://github.com/thlorenz/replpad
Why not load the file into an interactive node repl?
node -h
-e, --eval script evaluate script
-i, --interactive always enter the REPL even if stdin
node -e 'var client = require("./build/main/index.js"); console.log("Use `client` in repl")' -i
Then you can add to package.json scripts
"repl": "node -e 'var client = require(\"./build/main/index.js\"); console.log(\"Use `client` in repl\")' -i",
tested using node v8.1.2
Currently you can't do that directly, but you can mylib = require('./foo.js') in the REPL. Remember methods are exported, not declared as globals.
replpad is cool, but for a quick and easy way to load a file into node, import its variables and start a repl, you can add the following code to the end of your .js file
if (require.main === module){
(function() {
var _context = require('repl').start({prompt: '$> '}).context;
var scope = require('lexical-scope')(require('fs').readFileSync(__filename));
for (var name in scope.locals[''] )
_context[scope.locals[''][name]] = eval(scope.locals[''][name]);
for (name in scope.globals.exported)
_context[scope.globals.exported[name]] = eval(scope.globals.exported[name]);
})();
}
Now if your file is src.js, running node src.js will start node, load the file, start a REPL, and copy all the objects declared as var at the top level as well as any exported globals.
The if (require.main === module) ensures that this code will not be executed if src.js is included through a require statement. I fact, you can add any code you want to be excuted when you are running src.js standalone for debugging purposes inside the if statement.
Another suggestion that I do not see here: try this little bit of code
#!/usr/bin/env node
'use strict';
const repl = require('repl');
const cli = repl.start({ replMode: repl.REPL_MODE_STRICT });
cli.context.foo = require('./foo'); // injects it into the repl
Then you can simply run this script and it will include foo as a variable
Old answer
type test.js|node -i
Will open the node REPL and type in all lines from test.js into REPL, but for some reason node will quit after file ends
Another problem is, that functions will not be hoisted.
Better answer
node -e require('repl').start({useGlobal:true}); -r ./test2.js
Then all globals declared without var within test2.js will be available in the REPL
not sure why var a in global scope will not be available
There is an Official Node.js REPL that supports also async methods
console.js
const repl = require('repl')
const mongoose = require('mongoose')
const run = async () => {
await mongoose.connect(process.env.DB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const r = repl.start(`(${process.env.NODE_EN}) ⚡️ `)
r.context.User = require('./src/models/user.model')
r.context.mongoose = mongoose
console.log(`Ready 🚀`);
}
run()
Start the console:
NODE_OPTIONS=--experimental-repl-await node console.js
User model its exposed to console
await User.find({})
source

Categories

Resources