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

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

当我运行它时,我得到了错误

SyntaxError: Cannot use import statement outside a module

首先,我试图说服TPTB*这是一个模块(没有成功)。所以我把“import”改成了“require”,这就成功了。

但现在我在其他文件中有大约24个“导入”给我同样的错误。

*我敢肯定我问题的根源是我甚至不知道抱怨的是什么。我有点假设它是巴别塔7(因为我来自巴别塔6,我不得不改变预设),但我不是100%确定。

我发现的大多数解决方案似乎并不适用于直节点。比如这个:

ES6模块导入给出“未捕获SyntaxError:意外标识符”

说它是通过添加“type=module”来解决的,但这通常会在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",

当前回答

我是Node.js的新手,我在修复AWS Lambda函数(使用Node.js)时遇到了同样的问题。

我发现了CommonJS和ES6 JavaScript之间的一些差异:

ES6:

在包中添加"type":"module"。json文件 使用“import”从库中使用。 例如:从jwt-decode导入jwt_decode Lambda处理程序方法代码应该这样定义 “出口。Handler = async (event) => {}"

CommonJS:

不要在包中添加"type":"module"。json文件 使用“require”从lib中使用。 示例:const jwt_decode = require("jwt-decode"); lambda处理程序方法代码应该像这样定义: "export const handler = async (event) => {}"

其他回答

根据官方文件:

import语句只允许在ES模块中使用。要了解CommonJS中的类似功能,请参见import()。

为了让Node.js把你的文件当作ES模块,你需要(启用):

在package.json中添加"type": "module" 在Node.js调用中添加"——experimental-modules"标志

如果你正在使用ES6 JavaScript导入:

安装cross-env 在包中。将“test”:“jest”改为“test”:“cross-env NODE_OPTIONS=——experimental-vm-modules jest” 更多的包装。Json,添加这些:

    ...,
    "jest": {
        "transform": {}
    },
    "type": "module"

解释:

Cross-env允许在不更改NPM命令的情况下更改环境变量。接下来,在文件包中。你改变你的npm命令来启用实验性的ES6对Jest的支持,并配置Jest来做到这一点。

如果您正在使用node,请参考本文档。只要在你的节点应用程序中设置babel,它就会工作,它为我工作。

npm install --save-dev @babel/cli @babel/core @babel/preset-env

我也有同样的问题,下面已经修复了它(使用Node.js 12.13.1):

将.js文件扩展名更改为.mjs 在运行应用程序时添加——experimental-modules标志。 可选:在package.json中添加"type": "module"

更多信息:https://nodejs.org/api/esm.html

JavaScript模块文件的mime类型错误

该问题的常见根源是“模块”类型JavaScript文件的mime类型不能被处理或交付这些文件的服务器、客户端或ECMAScript引擎识别为“模块”类型。

The problem is the developers of Module JavaScript files incorrectly associated Modules with a new ".mjs" (.js) extension, but then assigned it a MIME-type server type of "text/javascript". This means both .js and .mjs types are the same. In fact the new type for .js JavaScript files has also changed to "application/javascript", further confusing the issue. So Module JavaScript files are not being recognized by any of these systems, regardless of Node.js or Babel file processing systems in development.

主要的问题是,大多数服务器或客户端(现代HTML5浏览器)还不知道这种新的JavaScript“模块”子类型。换句话说,除了JavaScript类型,他们没有办法知道Module文件类型到底是什么!

你得到了你发布的响应,JavaScript引擎说它需要知道这个文件是否是Module类型的JavaScript文件。

The only solution, for server or client, is to change your server or browser to deliver a new Mime-type that trigger ES6 support of Module files, which have an .mjs extension. Right now, the only way to do that is to either create a HTTP content-type on the server of "module" for any file with a .mjs extension and change your file extension on module JavaScript files to ".mjs", or have an HTML script tag with type="module" added to any external <script> element you use that downloads your external .js JavaScript module file.

一旦你欺骗浏览器或JavaScript引擎接受新的Module文件类型,它们就会开始在你使用的JS引擎或Node.js系统中进行脚本杂技表演。