我正在使用摩纳哥代码编辑器在 React 中制作一个代码编辑器 我正在尝试使用 prettier 在代码编辑器中格式化我的代码。 它给出错误' TypeError:无法读取未定义的属性(读取'语言') 在独立.mjs:22:694'
这是代码
import parser from "prettier/parser-babel";
try {
const test = async () => {
const code = `function add(a, b) {
return a + b;
}`;
const prettier = await import("prettier/standalone");
const formattedCode = await prettier.format(code, {
semi: true,
parser: "babel",
plugins: [parser],
useTabs: false,
});
console.log({ formattedCode });
};
test();
} catch (e) {
console.log({ e });
}
我也尝试使用不同的解析器,但它不起作用。
尝试使用这些导入:
import babelPlugin from "prettier/plugins/babel";
import estreePlugin from "prettier/plugins/estree";
我还导入了
estree
插件,因为你需要它来解析 JavaScript :-)