SyntaxError:无法在模块外部使用import语句 [英] SyntaxError: Cannot use import statement outside a module

查看:993
本文介绍了SyntaxError:无法在模块外部使用import语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个给我带来麻烦的ApolloServer项目,所以我认为我可能会更新它,并在使用最新的Babel时遇到问题.我的"index.js"是:

I've got an ApolloServer project that's giving me trouble, so I thought I might update it and ran into issues when using the latest Babel. My "index.js" is:

require('dotenv').config()
import {startServer} from './server'
startServer()

当我运行它时,出现错误"SyntaxError:无法在模块外部使用import语句".首先,我尝试做一些事情以说服TPTB *,这是一个模块(没有成功).因此,我将导入"更改为需要",并且可行.

And when I run it I get the error "SyntaxError: Cannot use import statement outside a module". First I tried doing things to convince TPTB* that this was a module (with no success). So I changed the "import" to a "require" and this worked.

但是现在我在其他文件中有大约两打导入",给了我同样的错误.

But now I have about two dozen "imports" in other files giving me the same error.

*我确定问题的根源是我什至不确定该问题的根源.我有点以为是Babel 7(因为我来自Babel 6,所以我不得不更改预设),但我不确定100%.

*I'm sure the root of my problem is that I'm not even sure what's complaining about the issue. I sort of assumed it was Babel 7 (since I'm coming from Babel 6 and I had to change the presets) but I'm not 100% sure.

我为解决方案找到的大多数内容似乎不适用于纯Node.像这里这样:

Most of what I've found for solutions don't seem to apply to straight Node. Like this one here:

ES6模块导入给出了"Uncaught SyntaxError:意外的标识符"

说可以通过添加"type = module"来解决,但这通常会出现在HTML中,而我没有.我还尝试使用项目的旧预设:

Says it was resolved by adding "type=module" but this would typically go in the HTML, of which I have none. I've also tried using my project's old presets:

"presets": ["es2015", "stage-2"],
"plugins": []

但是,这又给了我另一个错误:错误:插件/预设文件不允许导出对象,只能导出功能."

But that gets me another error: "Error: Plugin/Preset files are not allowed to export objects, only functions."

更新:这是我开始使用的依赖项:

UPDATE: Here are the dependencies I started with:

"dependencies": {
"@babel/polyfill": "^7.6.0",
"apollo-link-error": "^1.1.12",
"apollo-link-http": "^1.5.16",
"apollo-server": "^2.9.6",
"babel-preset-es2015": "^6.24.1",

推荐答案

验证您已安装了最新版本的Node(或至少为13.2.0+).然后执行以下操作之一,如文档中所述 :

Verify that you have the latest version of Node installed (or, at least 13.2.0+). Then do one of the following, as described in the documentation:

选项1

在最近的父package.json文件中,添加顶级"type"字段,其值为"module".这样可以确保将所有.js.mjs文件都解释为ES模块.您可以使用.cjs扩展名将单个文件解释为CommonJS.

In the nearest parent package.json file, add the top-level "type" field with a value of "module". This will ensure that all .js and .mjs files are interpreted as ES modules. You can interpret individual files as CommonJS by using the .cjs extension.

// package.json
{
  "type": "module"
}

选项2

以扩展名.mjs明确命名文件.所有其他文件(例如.js)都将解释为CommonJS,如果未在package.json中定义type,这是默认设置.

Explicitly name files with the .mjs extension. All other files, such as .js will be interpreted as CommonJS, which is the default if type is not defined in package.json.

这篇关于SyntaxError:无法在模块外部使用import语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆