简体   繁体   English

当我尝试导入本地模块时,React给出错误:不支持的错误

[英]React gives Error: not supported error when I try and import local module

I have a local module (speech.js) in my create-react-app src folder that is the google text to speech code on their website. 我的create-react-app src文件夹中有一个本地模块(speech.js),这是他们网站上的Google文本到语音的代码。 I adjusted it to be an arrow function and use that specific export syntax. 我将其调整为箭头功能,并使用该特定的导出语法。

const textToSpeech = require('@google-cloud/text-to-speech');

// Import other required libraries
const fs = require('fs');
const util = require('util');
export const main = async () => {
  // Creates a client
  const client = new textToSpeech.TextToSpeechClient();

  // The text to synthesize
  const text = "Hello world";

  // Construct the request
  const request = {
    input: {text: text},
    // Select the language and SSML Voice Gender (optional)
    voice: {languageCode: 'en-US', ssmlGender: 'NEUTRAL'},
    // Select the type of audio encoding
    audioConfig: {audioEncoding: 'MP3'},
  };

  // Performs the Text-to-Speech request
  const [response] = await client.synthesizeSpeech(request);
  // Write the binary audio content to a local file
  const writeFile = util.promisify(fs.writeFile);
  await writeFile('output.mp3', response.audioContent, 'binary');
  console.log('Audio content written to file: output.mp3');
};

What I'm not understanding is why this syntax isn't working in App.js. 我不明白的是为什么该语法在App.js中不起作用。 import {main} from './speech';

I get the error, Error: not support and "4 stack frames were collapsed". 我收到错误, Error: not support并且“ 4个堆栈框架已折叠”。 Quite informative! 内容丰富!

Does anyone know what the error could be here? 有人知道错误可能在这里吗? I thought as long as I used es6 style imports and exports I wouldn't receive errors. 我以为只要使用es6样式的导入和导出,我都不会收到错误。 Could this be due to the first require() statement of speech.js? 这可能是由于Speech.js的第一个require()语句引起的吗? Any help would be appreciated. 任何帮助,将不胜感激。 I've felt like banging my head against the wall for the past 40 minutes. 在过去的40分钟里,我一直想把头撞在墙上。

May not be the correct answer but I believe it has a good chance of being right. 可能不是正确的答案,但我相信它很有可能是正确的。 I believe that since node is just a runtime environment and not a part of the actual browser, you aren't able to use node modules with react (a frontend framework). 我相信,由于节点只是运行时环境,而不是实际浏览器的一部分,因此您无法将节点模块与react(前端框架)一起使用。 The solution to this quandary would be to use something like electron. 解决这一难题的方法是使用电子。

暂无
暂无

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

相关问题 当我尝试创建一个反应应用程序时,它会出错 - when I try to create a react app it gives an error 当我尝试需要本地模块时,node.js 抛出错误 - node.js throws error when I try to require local module 当我尝试使用npm安装任何模块时,它将引发错误EMAXREDIRECT - When I try to install any module with npm, it throws error EMAXREDIRECT 当我尝试创建 react 项目时出现错误 - When i try to create react project i get error 尝试将获取请求从控制器模块导入到主应用程序时出现错误。 (类型错误:无法读取未定义的正确“获取”) - I get an error when I try to import a get request from a controller module to my main app. (Type-error : Cannot read proper 'get' of undefined) Npm 尝试创建反应应用程序时出错 - Npm Error when I try to create the react app 当我尝试使用npm start命令运行React App时出错 - Error when I try run a React App with npm start command 尝试在AWS上运行React Web时出现网络错误 - Network Error when I try to run React web on AWS 我正在为 node.js 使用 ES6 语法,并且我已导出模块但尝试导入时出现错误无法找到模块 - I am using ES6 syntax for node.js and I have exported module but trying to import gives error Cannot find module 当我尝试使用npm在win8中安装某些软件包时,总是会出现这样的错误 - when i try to use npm to install some package in my win8, it always gives me error like this
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM