简体   繁体   English

节点 js SyntaxError:意外的令牌“导出”

[英]Node js SyntaxError: Unexpected token 'export'

在此处输入图像描述

I'm having the following error in the console when I try to run my api written in node js.当我尝试运行用节点 js 编写的 api 时,控制台中出现以下错误。

Can you tell me which is the problem.你能告诉我是哪个问题吗。

Importing is generally done at the top of your files and looks like this:导入通常在文件顶部完成,如下所示:

import User, {schema} from './model.js'

Exporting is generally done at the bottom of your files and looks like this:导出通常在文件底部完成,如下所示:

module.exports = {User, schema}

Ant the specific reason why you get the error is because export is not used that way. Ant 你得到错误的具体原因是因为export不是那样使用的。

This directly quoted from Web Docs :这直接引用自Web Docs

export DefaultExport from 'bar.js';从“bar.js”导出 DefaultExport; // Invalid // 无效的

If you want to include a structure you have built, you need to use import.如果要包含已构建的结构,则需要使用import。 If you want to export a structure that you have edited, you need to use export.如果要导出已编辑的结构,则需要使用导出。

You are using ES6 syntax.您正在使用 ES6 语法。

Node.js uses common.js syntax Node.js 使用 common.js 语法

You can use module.export syntax or you can use babel npm package for translate ES6 syntax to common.js syntax您可以使用 module.export 语法,也可以使用 babel npm package 将 ES6 语法转换为 common.js 语法

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM