【发布时间】:2019-07-04 17:54:34
【问题描述】:
我的 create-react-app src 文件夹中有一个本地模块 (speech.js),它是他们网站上的 google 文本到语音代码。我将其调整为箭头函数并使用特定的导出语法。
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');
};
我不明白为什么这种语法在 App.js 中不起作用。
import {main} from './speech';
我收到错误消息,Error: not support 和“4 个堆栈帧已折叠”。信息量很大!
有谁知道这里的错误可能是什么?我以为只要我使用 es6 样式的导入和导出,我就不会收到错误。这可能是由于 Speech.js 的第一个 require() 语句造成的吗?任何帮助,将不胜感激。在过去的 40 分钟里,我一直想把头撞在墙上。
【问题讨论】:
-
检查 babel 预设。您可以在根目录下创建 .babelrc 并在其中添加预设。
-
@SameerRezaKhan 感谢您的回复。预设应该是什么?
-
如果你的项目中有最新的 babel,preset 应该是 @babel/preset-react 和 @babel/preset-env
-
@SameerRezaKhan 我应该对这个文件做些什么吗?我在项目根目录中创建它,但我不确定如何在我的项目中有效地使用它。
标签: node.js reactjs import export