我有一个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,请参考本文档。只要在你的节点应用程序中设置babel,它就会工作,它为我工作。

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

其他回答

I had the same problem when I started to use Babel... But later, I had a solution... I haven't had the problem any more so far... Currently, Node.js v12.14.1, "@babel/node": "^7.8.4", I use babel-node and nodemon to execute (Node.js is fine as well..) package.json: "start": "nodemon --exec babel-node server.js "debug": "babel-node debug server.js"!! Note: server.js is my entry file, and you can use yours. launch.json. When you debug, you also need to configure your launch.json file "runtimeExecutable": "${workspaceRoot}/node_modules/.bin/babel-node"!! Note: plus runtimeExecutable into the configuration. Of course, with babel-node, you also normally need and edit another file, such as the babel.config.js/.babelrc file

首先,我们将安装 @babel/cli、@babel/core 和 @babel/preset-env:

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

然后我们将创建一个.babelrc文件来配置Babel:

touch .babelrc

这将承载我们可能想要配置Babel的任何选项:

{
  "presets": ["@babel/preset-env"]
}

随着Babel最近的变化,你需要在Node.js运行ES6之前编译它。

我们将在package.json文件中添加第一个脚本build。

"scripts": {
  "build": "babel index.js -d dist"
}

然后我们将在package.json文件中添加开始脚本。

"scripts": {
  "build": "babel index.js -d dist", // replace index.js with your filename
  "start": "npm run build && node dist/index.js"
}

现在让我们启动服务器。

npm start

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

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

以下是节选:

“更新2020

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

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

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

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

我在一个刚刚起步的Express API项目中遇到了这个问题。

src/server/server.js中有问题的服务器代码:

import express from 'express';
import {initialDonationItems, initialExpenditureItems} from "./DemoData";

const server = express();

server.get('/api/expenditures', (req, res) => {
  res.type('json');
  res.send(initialExpenditureItems);
});

server.get('/api/donations', (req, res) => {
  res.type('json');
  res.send(initialDonationItems);
});

server.listen(4242, () => console.log('Server is running...'));

以下是我的依赖项:

{
  "name": "contributor-api",
  "version": "0.0.1",
  "description": "A Node backend to provide storage services",
  "scripts": {
    "dev-server": "nodemon --exec babel-node src/server/server.js --ignore dist/",
    "test": "jest tests"
  },
  "license": "ISC",
  "dependencies": {
    "@babel/core": "^7.9.6",
    "@babel/node": "^7.8.7",
    "babel-loader": "^8.1.0",
    "express": "^4.17.1",
    "mysql2": "^2.1.0",
    "sequelize": "^5.21.7",
    "sequelize-cli": "^5.5.1"
  },
  "devDependencies": {
    "jest": "^25.5.4",
    "nodemon": "^2.0.3"
  }
}

这是投出错误的跑者:

nodemon --exec babel-node src/server/server.js --ignore dist

这很令人沮丧,因为我有一个类似的Express项目运行得很好。

解决方案首先是添加这个依赖:

npm install @babel/preset-env

然后在项目根目录中使用babel.config.js来连接它:

module.exports = {
  presets: ['@babel/preset-env'],
};

我不完全明白为什么这是可行的,但我是从权威来源复制的,所以我很高兴坚持下去。