我有一个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",

当前回答

我试了所有的方法,但都没用。

我从GitHub上得到了一个参考。

为了在Node.js中使用TypeScript导入,我安装了以下包。

1. npm i typescript --save-dev

2. npm i ts-node --save-dev

在package.json中不需要type: module

例如,

{
  "name": "my-app",
  "version": "0.0.1",
  "description": "",
  "scripts": {

  },
  "dependencies": {
    "knex": "^0.16.3",
    "pg": "^7.9.0",
    "ts-node": "^8.1.0",
    "typescript": "^3.3.4000"
  }
}

其他回答

对于那些和我一样在阅读答案时感到困惑的人,在你的包裹里。Json文件,添加 “类型”:“模块” 在上层如下图所示:

{
  "name": "my-app",
  "version": "0.0.0",
  "type": "module",
  "scripts": { ...
  },
  ...
}

我最近遇到了这个问题。对我有效的修复是将此添加到插件部分的babel.config.json文件中:

["@babel/plugin-transform-modules-commonjs", {
    "allowTopLevelThis": true,
    "loose": true,
    "lazy": true
  }],

我有一些导入模块与//和错误“不能使用导入模块外”。

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

我是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”和“require”

一些较新的ES6模块只能使用导入。 一些CommonJS使用require。

以下是对我有效的方法:

把你的js文件转换成.mjs,就像其他答案中建议的那样 "require"在ES6模块中没有定义,所以你可以这样定义它: 从模块导入{createRequire} const require = createRequire(import.meta.url); 现在'require'可以用在通常的情况下。 对于ES6模块使用import,对于CommonJS使用require。

一些有用的链接:Node.js自己的文档。import和require的区别。Mozilla有一些关于导入的很好的文档