[英]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.