我有一个 ApolloServer 项目出了些问题,所以我想更新它,并在使用最新的 Babel 时遇到了一些问题。我的 "index.js" 文件如下:
require('dotenv').config()
import {startServer} from './server'
startServer()
当我运行它时,我收到了错误信息:
SyntaxError: Cannot use import statement outside a module
首先,我尝试了一些方法来说服TPTB*这是一个模块(但没有成功)。所以我把"import"改成了"require",这样就可以了。
但现在我有大约两打其他文件中的"imports"也给我同样的错误。
*我确定我的问题的根源是我甚至不知道是什么在抱怨这个问题。我有点假设它是Babel 7(因为我从Babel 6过来,我必须改变预设),但我不是100%确定。
我找到的大部分解决方案似乎都不适用于纯Node。就像这个:
ES6 module Import giving "Uncaught SyntaxError: Unexpected identifier"
说通过添加"type=module"来解决,但这通常会放在HTML中,而我没有HTML。我还尝试使用我的项目的旧预设:
"presets": ["es2015", "stage-2"],
"plugins": []
但是这给我带来了另一个错误:“错误:插件/预设文件不允许导出对象,只能导出函数。”
以下是我开始使用的依赖项:
"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",
"type":"module"
并不能解决我的问题,而且有超过一百个imports
。require
的语法与imports
不同,不是很容易替换。你能否给出一个将imports
替换为requires
的示例? - Jeb50