简体   繁体   English

Node.JS“未捕获的 TypeError:util.TextEncoder 不是构造函数”

[英]Node.JS "Uncaught TypeError: util.TextEncoder is not a constructor"

I have used browserify to bundle up my js files.我已经使用 browserify 来捆绑我的 js 文件。 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这是我最初的几行 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 ] Output:Uint8Array(5)[72、101、108、108、111]

Try by running npm install text-encoding尝试运行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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Node.js - 类型错误:客户端不是构造函数 - Node.js - TypeError: Client is not a constructor 未捕获的TypeError:无法读取null Node.js的属性&#39;getContext&#39; - Uncaught TypeError: Cannot read property 'getContext' of null Node.js Node.js 基本错误:Uncaught TypeError: Binance is not a function - Node.js basic error: Uncaught TypeError: Binance is not a function Node.js 未捕获的异常类型错误:无法设置未定义的 preoprty &#39;&#39; - Node.js uncaught exception typeError: cannot set preoprty '' of undefined 未捕获的类型错误:无法读取 node.js 中未定义的属性“通道” - Uncaught TypeError: Cannot read property 'channel' of undefined in node.js Moustrap node.js; 未捕获的TypeError:回调不是函数 - Moustrap node.js; Uncaught TypeError: callback is not a function 未捕获的类型错误:无法读取 node.js 上未定义的属性“长度” - Uncaught TypeError: Cannot read property 'length' of undefined on node.js TypeError: Class extends value undefined is not a constructor 或 null - Node.js - TypeError: Class extends value undefined is not a constructor or null - Node.js TypeError: Class extends value 命令不是构造函数或 null - node.js - TypeError: Class extends value Command is not a constructor or null - node.js Node.js 中的类型错误 - TypeError in Node.js
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM