我有一个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在我的计算机上执行一些本地任务。

让我们假设我的旧脚本是test.js。在里面,如果我想用的话

import something from "./mylocalECMAmodule";

它会抛出一个这样的错误:

(node:16012) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
SyntaxError: Cannot use import statement outside a module
...

这不是模块错误,而是Node.js错误。禁止加载“模块”之外的任何东西。

要解决这个问题,只需将旧脚本test.js重命名为test.mjs。

这是所有。

其他回答

文档很混乱。我使用Node.js在我的计算机上执行一些本地任务。

让我们假设我的旧脚本是test.js。在里面,如果我想用的话

import something from "./mylocalECMAmodule";

它会抛出一个这样的错误:

(node:16012) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
SyntaxError: Cannot use import statement outside a module
...

这不是模块错误,而是Node.js错误。禁止加载“模块”之外的任何东西。

要解决这个问题,只需将旧脚本test.js重命名为test.mjs。

这是所有。

我的解决方案是在运行nodemon时包含babel-node路径,如下所示:

nodemon node_modules/.bin/babel-node index.js

你可以加入你的套餐。Json脚本如下:

debug: nodemon node_modules/.bin/babel-node index.js

注意:我的入口文件是index.js。将其替换为您的入口文件(许多有app.js/server.js)。

取出"module": "esnext"并确保" modulerresolve ": "node"在其中,这个特定的组合为我做了

我发现这个链接中的2020年更新的答案有助于回答这个问题,并告诉你为什么它会这样做:

使用Node.js require vs. ES6导入/导出

以下是节选:

“更新2020

自Node v12以来,默认情况下启用了对ES模块的支持,但在撰写本文时仍处于试验阶段。包含节点模块的文件必须以.mjs或最近的包结尾。Json文件必须包含“type”:“module”。Node文档有更多的信息,也包括CommonJS和ES模块之间的互操作。”

节点v14.16.0 对于那些尝试过。mjs的人来说:

Aviator@AW:/mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex$ node just_js.mjs
file:///mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex/just_js.mjs:3
import fetch from "node-fetch";
       ^^^^^

SyntaxError: Unexpected identifier

谁试过从"node-fetch"导入取回; 谁试过const fetch = require('node-fetch');

Aviator@AW:/mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex$ node just_js.js
(node:4899) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/mnt/c/Users/Adrian/Desktop/Programming/nodejs_ex/just_js.js:3
import fetch from "node-fetch";
^^^^^^

SyntaxError: Cannot use import statement outside a module  

那些尝试过"type": "module"来打包的人。Json,但继续看到错误,

{
  "name": "test",
  "version": "1.0.0",
  "description": "to get fetch working",
  "main": "just_js.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "MIT"
}

我可以毫无问题地切换到axios。

从'axios'导入axios;<——置于文件顶部。 例子:

axios.get('https://www.w3schools.com/xml/note.xml').then(resp => {

    console.log(resp.data);
});