javascript - Node.JS "Uncaught TypeError: util.TextEncoder is not a constructor" - Stack Overflow

I have used browserify to bundle up my js files. I get an error of the formUncaught TypeError: util.Te

I have used browserify to bundle up my js files. I get an error of the form

Uncaught TypeError: util.TextEncoder is not a constructor
    at Object.1.util (bundle.js:3)
    at o (bundle.js:1)
    at r (bundle.js:1)
    at bundle.js:1

Here are my initial few lines of bundle.js

(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
var util = require('util');
var utf8 = new util.TextEncoder();

let secretDataTxt=document.querySelector(`#SecretData`);
let passwordTxt=document.querySelector(`#Password`);
let result=document.querySelector(`#Result`);

let encryptBtn=document.querySelector(`#encrypt`);

Can someone please help me with this error?

EDIT: Here is a working example

const util = require('util');
const utf8 = new util.TextEncoder();

console.log(utf8.encode("Hello"));

Output: Uint8Array(5) [ 72, 101, 108, 108, 111 ]

I have used browserify to bundle up my js files. I get an error of the form

Uncaught TypeError: util.TextEncoder is not a constructor
    at Object.1.util (bundle.js:3)
    at o (bundle.js:1)
    at r (bundle.js:1)
    at bundle.js:1

Here are my initial few lines of bundle.js

(function(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){
var util = require('util');
var utf8 = new util.TextEncoder();

let secretDataTxt=document.querySelector(`#SecretData`);
let passwordTxt=document.querySelector(`#Password`);
let result=document.querySelector(`#Result`);

let encryptBtn=document.querySelector(`#encrypt`);

Can someone please help me with this error?

EDIT: Here is a working example

const util = require('util');
const utf8 = new util.TextEncoder();

console.log(utf8.encode("Hello"));

Output: Uint8Array(5) [ 72, 101, 108, 108, 111 ]

Share Improve this question edited Sep 1, 2020 at 18:17 Chinmay Vemuri asked Sep 1, 2020 at 17:48 Chinmay VemuriChinmay Vemuri 1991 gold badge2 silver badges11 bronze badges 8
  • This is the offending line: var utf8 = new util.TextEncoder(); Are you sure that util is defined? I'm not familiar with browserify, but normally require is not available on the window object, (just in node.js) – paroxyzm Commented Sep 1, 2020 at 17:52
  • @paroxyzm I don't think there is a problem with util. It works fine elsewhere. – Chinmay Vemuri Commented Sep 1, 2020 at 18:06
  • Try this code, it will error with the same message as yours const x = undefined const r = new x() – paroxyzm Commented Sep 1, 2020 at 18:08
  • @paroxyzm Yeah it does but what is that got to do with util ? – Chinmay Vemuri Commented Sep 1, 2020 at 18:17
  • if util does not have TextEncoder then you'll have this error. So maybe utils is malformed? Your code behaves exactly as if util was defined like this util = {} – paroxyzm Commented Sep 1, 2020 at 18:21
 |  Show 3 more ments

1 Answer 1

Reset to default 3

Try by running npm install text-encoding

and paste this code wherever it gives you the error:

const TextEncodingPolyfill = require('text-encoding');
Object.assign(global, {
  TextEncoder: TextEncodingPolyfill.TextEncoder,
  TextDecoder: TextEncodingPolyfill.TextDecoder,
});


const encoder = new TextEncoder();
const decoder = new TextDecoder();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744207199a4563161.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信