I have react-scripts version 5.0.1 in package.json
before I updated to 5.0.1 it was working fine in IE.
Now I am getting
object doesn't support property or method 'endsWith'
error
replace it with this
const str = 'Is this a question?';
console.log(str.slice(str2.length -1) === '?' );
I know how to index an array with [] but I'm trying to display an item from an array based on its index using Array.at() method as described here MDN Docs - Array.at
But I get the following error:
Uncaught TypeError: arr1.at is not a function
I double-checked it, and everything is ok, however I don't know what's going wrong.
Here is my code:
const arr1 = [10, 20, 30, 40, 50];
const res = arr1.at(2);
console.log(res);
Note: This is different than the proposed duplicate of How to get value at a specific index of array In JavaScript?. That question is about methods for accessing an array, this question is why a new API for doing so is unavailable and how to rectify that.
If you get this message, whatever platform you're running the code on does not support the method yet. It's quite new - while the most recent versions of most browsers support it, anything before 2021 definitely won't. This method was only very recently signed off on (end of August 2021) and incorporated into the official specification, so it won't exist in older environments. Either upgrade your environment, or add a polyfill.
Per the proposal document, a "rough polyfill" that should be standards-compliant for most cases is:
function at(n) {
// ToInteger() abstract op
n = Math.trunc(n) || 0;
// Allow negative indexing from the end
if (n < 0) n += this.length;
// OOB access is guaranteed to return undefined
if (n < 0 || n >= this.length) return undefined;
// Otherwise, this is just normal property access
return this[n];
}
const TypedArray = Reflect.getPrototypeOf(Int8Array);
for (const C of [Array, String, TypedArray]) {
Object.defineProperty(C.prototype, "at",
{ value: at,
writable: true,
enumerable: false,
configurable: true });
}
Simply run that before trying to use .at, and you should be able to use it, even on older incompatible environments. You can also install this more exhaustive shim instead if you wish.
One option is to use the core-js library...
Modular standard library for JavaScript. Includes polyfills for
ECMAScript up to 2021: promises, symbols, collections, iterators,
typed arrays, many other features, ECMAScript proposals, some
cross-platform WHATWG / W3C features and proposals like URL. You can
load only required features or use it without global namespace
pollution.
First install it with npm or yarn:
npm install core-js
or
yarn add core-js
Then use it in your JS or TS project like this:
import 'core-js/features/array/at';
let arr = [];
arr.push(42);
console.log(arr.at(0));
Note that the code above only imports the at method for array. To import all polyfills or a subset:
// polyfill all `core-js` features, including early-stage proposals:
import "core-js";
// or:
import "core-js/features";
// polyfill all actual features - stable ES, web standards and stage 3 ES proposals:
import "core-js/actual";
// polyfill only stable features - ES and web standards:
import "core-js/stable";
// polyfill only stable ES features:
import "core-js/es";
If you're using node.js then please check the version of node.
It should be greater than 16.6.0. If not then update the node.js version.
Browser compatibility of Array.prototype.at()
The code is working fine in Stackoverflow code terminal. Your machine might not support the same due to JS versioning. The method is introduced recently.
I ran into this same issue when using Pipedream. I didn't realize that at() wasn't included in the NodeJS version they're using hinted at by Li Ki.
I implemented a similar solution proposed by Bjørnar Hvidsten; however, to get it to work in Pipedream, I simply added the following to the top of my NodeJS code.
import 'core-js'
const myArray = [1,2,3]
console.log(typeof myArray.at) // "function"
P.S. Referencing specific parts of core-js resulted in errors inside Pipedream, at least how I was trying to call it.
When I try to execute util.format with inspect as an object returning a function like
util.format({inspect: function() { return 1; } })
Node 10 output:
'1'
Node 11 output:
'{ inspect: [Function: inspect] }'
I couldn't find anything in the documentation in regards to using util.format having an inspect as key. I am not sure why the behaviour is different.
PS: Couldn't have REPL with a older version, so I couldn't attach a reproducible version. But running this code in different node version would show the logs as mentioned in the example.
Using this code in Node.js v10 gives:
(node:39468) [DEP0079] DeprecationWarning: Custom inspection function on Objects via .inspect() is deprecated
You should use util.inspect.custom instead of util.format
See Deprecations
I'm trying to get the source code location of node.
There is parse5 (use by cheerio) sourceCodeLocationInfo option.
But with this test code :
const cheerio = require('cheerio');
const $ = cheerio.load('<html><head><title>Test page</title></head><body></body></html>', { sourceCodeLocationInfo: true })
let title = $('title');
console.log(title.text());
console.log(title.sourceCodeLocation);
sourceCodeLocation property of title is undefined.
Any ideas?
This is because the Cheerio version on NPM is not up to date.
Version 1.0.0-rc.3 is available and not version 1.0.0.
From GitHub, I installed version 1.0.0 this way npm install https://github.com/cheeriojs/cheerio.git#v1.0.0. Everything is OK.
I am trying to use the window.crypto.getRandomValues method in a nodejs script. From my understanding there is no window element when I run a simple code like this in node:
var array = new Uint32Array(10);
window.crypto.getRandomValues(array);
Which is why I get this error:
ReferenceError: window is not defined
How can I use this method in my code?
Thanks
You can use the built-in crypto module instead. It provides both a crypto.randomBytes() as well as a crypto.pseudoRandomBytes().
However it should be noted that these methods give you a Buffer object, you cannot pass in a Uint32Array or similar, so the API is a bit different.
const crypto = require('crypto').webcrypto;
let a = new Uint8Array(24);
console.log(crypto.getRandomValues(a));
This works almost exactly like the one in the browser, by adding webcrypto to the end of requrie('crypto');.
You can use this module which is the same as the window element: get-random-values
Install it:
npm install get-random-values --save
Use it:
var getRandomValues = require('get-random-values');
var array = new Uint32Array(10);
getRandomValues(array);
Here is how to use it in Node 16 with TypeScript. I'm hijacking the web types and overriding the #types/node type, which are missing webcrypto.
import { webcrypto } from 'crypto'
const crypto = webcrypto as unknown as Crypto
const random = crypto.getRandomValues(new Uint8Array(24))
This sandbox will work in Node 16, but stackblitz won't release node 16 for another couple months. https://stackblitz.com/edit/koa-starter-wychx9?file=package.json
Issue: github.com/denoland/node_deno_shims/issues/56
I had this problem too, I solved it this way
import * as crypto from 'node:crypto'
export function randomChar() {
return crypto.webcrypto.getRandomValues(new BigUint64Array(1))[0].toString(36)
}
reference:
How to use getRandomValues() in nodejs?