【问题标题】:ReferenceError: TextEncoder is not definedReferenceError:未定义 TextEncoder
【发布时间】:2013-10-31 03:16:37
【问题描述】:

我正在 Linux 上的 Firefox - 24 中编写一个简单的插件。 我得到错误:

ReferenceError: TextEncoder is not defined

当我这样做时: var encoder = new TextEncoder(); 我正在使用的功能是:

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(); 
    });
}

【问题讨论】:

    标签: javascript firefox-addon firefox-addon-sdk


    【解决方案1】:

    如果您在运行节点服务器时遇到此错误

    找到这个文件node_modules/whatwg-url/dist/encoding.js

    在顶部添加这一行const { TextEncoder, TextDecoder } = require("util");

    【讨论】:

    • 这可行,但路径已更改为node_modules/whatwg-url/lib/encoding.js
    • 添加行后 const { TextEncoder, TextDecoder } = require("util"),.运行 npm install。
    【解决方案2】:

    在nodejs中你可以用util解决:

    var util= require('util');
    var encoder = new util.TextEncoder('utf-8');
    

    【讨论】:

      【解决方案3】:

      如果您因为通过 npm install mongodb 使用 Mongodb 而遇到这种情况,那么最简单的方法就是升级您的节点版本。需要高于12版本;我使用的是第 16 版,它清楚地解决了我的问题

      【讨论】:

      • 我试图安装依赖项的项目使用了更高版本的 NodeJS,而没有指定使用的 NodeJS 版本。我需要添加一个 nvm 配置文件并指定最新版本的 NodeJS。我的本地版本的 NodeJS 使用的是旧版本的 NodeJS。
      • 谢谢,更新 Nodejs 解决了这个问题。我使用的是 Ubuntu 20,默认安装 node v10。
      【解决方案4】:

      啊,您正在使用 SDK,我在重新阅读您的其他问题的实际错误时收集到。

      • 您需要从其他模块显式导入 TextEncoder,因为 SDK 模块缺少该类。
      • 你需要yieldOS.File.open。
      • append: 仅在 Firefox 27+ 中受支持
      • .flush() 仅在 Firefox 27+ 中受支持(无论如何都是个坏主意)。如果需要,请使用 .writeAtomic
      • write: true 写入文件。

      这是我在 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");
      

      【讨论】:

        【解决方案5】:

        我也遇到了这个错误,所以我用这种方式解决了它, 在 nodejs 项目中转到 node_modules/whatwg-url/dist/encoding.js 文件中添加这一行 =>

        const {TextDecoder, TextEncoder} = require("util");
        

        你的问题解决了

        【讨论】:

        • 如果您需要重新安装 npm 包或更新等,这将是一个问题。您将如何跟踪此更改?
        【解决方案6】:
        1. 此问题仅在节点 10 或更低版本中出现。 要解决此问题upgrade node version to 12 or higher 然后rm -rf node_modules && npm i

        2. 或者如果你不想升级node版本,那么,

          找到这个文件

          node_modules/whatwg-url/dist/encoding.js // If dist folder
          
          node_modules/whatwg-url/lib/encoding.js // If lib folder
          

          并在 encoding.js 文件中添加这一行

          const { TextEncoder, TextDecoder } = require("./utils"); // if utils file
          
          const { TextEncoder, TextDecoder } = require("./util"); // if util file
          

        【讨论】:

          【解决方案7】:

          Node.js 的文本编码器可以在 util module 中找到。你可以这样访问它:

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

          TextEncoder 的作用之一是将文本字符串转换为字节数组。你可以这样实现:

          const data = TextEncoder.encode(
             JSON.stringify({ c: "Green" })
          );
          // Uint8Array [ 123, 34, 99, 34, 58, 34, 71, 114, 101, 101, 110, 34, 125 ]
          

          返回的数组称为Uint8Array。它由 0 到 255 范围内的整数组成。

          注意TextEncoder 只支持UTF-8 编码。

          【讨论】:

            【解决方案8】:

            如果 node_modules/whatwg_url/dist/encoding.js 文件夹中有错误,则卸载 MongoDB

            npm uninstall mongodb
            

            然后重新安装

            npm install --save mongodb
            

            【讨论】:

            • 尝试通过 npm install mongoose 安装 mongoose
            • 写给猫鼬:从 v6.x 版本开始,它需要 NodeJS >= v12.0.0
            【解决方案9】:

            对我来说升级 Node.js 版本解决了这个问题

            【讨论】:

              【解决方案10】:

              TextEncoder 可以在sdk/io/buffer 模块中找到:

              let { TextEncoder, TextDecoder } = require('sdk/io/buffer')
              

              【讨论】:

                【解决方案11】:

                我在我的项目中也遇到了同样的问题,但我通过将我的 节点版本10 升级到 12 解决了这个问题。由于我们在项目中使用的节点版本较低,现在可能会出现这个问题。

                【讨论】:

                  【解决方案12】:

                  这看起来像是节点版本错误,因为我通过从 10 更新到 16 解决了它,然后我安装了依赖项并打开了一个新终端。

                  1. 将节点更新到 14 或更高版本,我使用了 Node Version Manager (NVM)

                  2. 删除 node_modules 目录,在 linux 上:

                    rm -rf 节点模块

                  3. 使用 npm install 安装依赖项

                  4. 关闭并打开一个新终端

                  5. 使用 node 或 nodemon 运行应用程序

                  完成!

                  【讨论】:

                    【解决方案13】:

                    TextEncoder 是 javascript 中的原生函数,请检查适合该能力的版本。

                    https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder#browser_compatibility

                    Chrome 版本 >=38、Edge 版本 >=79、Firefox 版本 >=18、Node 版本 >=11.0.0...

                    【讨论】:

                      【解决方案14】:

                      删除你的“node_modules”文件夹

                      rm -rf node_modules
                      

                      然后重新安装依赖项

                      npm i
                      

                      如果您使用的 'mongoose' 大于 v6,则至少需要 Node.js v12

                      【讨论】:

                        【解决方案15】:

                        我遇到了同样的错误,因为必须安装旧的 nodejs。这个问题可以通过安装最新的nodejs来解决。 将 nodejs 更新到 nodejs 到 14.x

                        sudo apt 更新 卷曲-sLhttps://deb.nodesource.com/setup_14.x |须藤重击 - sudo apt install -y nodejs 节点-v

                        【讨论】:

                          【解决方案16】:

                          在 node_modules>whatwg-url>dist 中打开您的 encoding.js 文件夹

                          代替:

                          "use strict";
                          const utf8Encoder = new TextEncoder();
                          const utf8Decoder = new TextDecoder("utf-8", { ignoreBOM: true });
                          

                          写下这段代码:

                          "use strict";
                          var util= require('util');
                          const utf8Encoder = new util.TextEncoder();
                          const utf8Decoder = new util.TextDecoder("utf-8", { ignoreBOM: true });
                          

                          通过包含 utils,您所缺少的只是这一小部分。

                          【讨论】:

                            猜你喜欢
                            • 1970-01-01
                            • 2019-09-16
                            • 1970-01-01
                            • 2012-08-24
                            • 2015-10-04
                            • 2017-04-10
                            • 1970-01-01
                            相关资源
                            最近更新 更多