获得 "Cannot use import statement outside a module" 即使我在 package.json 中有 '"type": "module"'

Getting "Cannot use import statement outside a module" even if I have '"type": "module"' in package.json

我有一个简单的 express 后端,我想测试一下。当使用 GET 命中 /messages 时,后端只是 returns 一个消息数组。

注意:我知道可能有一个简单的修复方法,因为我是 node.js 世界的新手,之前主要在 Python 环境中工作。所以,请提供解释的答案。

这是我的 package.json:

{
  "name": "backend",
  "version": "1.0.0",
  "description": "",
  "type": "module",
  "main": "index.js",
  "scripts": {
    "dev": "nodemon server.js",
    "test": "jest"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "cors": "^2.8.5",
    "express": "^4.17.1"
  },
  "devDependencies": {
    "jest": "^27.3.1",
    "supertest": "^6.1.6"
  }
}

注意事项:

  1. "type": "module".
  2. 我正在使用 jest 和 supertest 来测试我的支持 express 的后端。

让我们看看我的 server.js 长什么样:

import app from './app.js'

const port = 3000

app.listen(port, () => console.log('app running'))

出于测试目的,在server.js中我只有启动服务器的代码。所有重要的代码都在 app.js.

import express from 'express'
import cors from 'cors'
const app = express()

app.use(cors())
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

const messages = ['hello', 'hi', 'its working']

app.get('/messages', (req, res) => {
  res.send(messages)
})

export default app

注意事项:

  1. export default app

在继续向您展示我的 server.test.js 之前,让我们先看看我的目录结构。

├── app.js
├── package.json
├── package-lock.json
├── server.js
└── tests
    └── server.test.js

1 directory, 5 files

现在,这是我的 server.test.js:

import app from '../app'
import supertest from 'supertest'
const requestWithSupertest = supertest(app);


describe('Messages Endpoints', () => {

  it('GET /messages should show all messages', async () => {
    const res = await requestWithSupertest.get('/messages');
    expect(res.status).toEqual(200);
    expect(res.type).toEqual(expect.stringContaining('json'));
    expect(res.body).toHaveProperty('messages')
  });

});

注意事项:import app from '../app'。我正在从名为 app.js 的文件中导入应用程序,该文件是树中的一个目录,如上面 tree 输出所示。

现在我已经设置好了所有内容,接下来我将继续调用测试。我收到 jest runner 抛出的错误。我删除了一些输出以仅显示主要错误:

 FAIL  tests/server.test.js
  ● Test suite failed to run

    Jest encountered an unexpected token

    [output trimmed...]

    Details:

    /efs/repos/vueandauth/backend/tests/server.test.js:1
    ({"Object.<anonymous>":function(module,exports,require,__dirname,__filename,jest){import app from '../app';
                                                                                      ^^^^^^

    SyntaxError: Cannot use import statement outside a module

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at TestScheduler.scheduleTests (node_modules/@jest/core/build/TestScheduler.js:333:13)

Test Suites: 1 failed, 1 total
Tests:       0 total
Snapshots:   0 total
Time:        1.149 s
Ran all test suites.

我在 Whosebug 上搜索了其他问题,其中一些建议在 package.json 中做一个 "type": "module"。我已经做过了。为什么会出现此错误以及如何解决此问题?

关于 ECMAScript 模块 的开玩笑支持,你可以看看这个 GitHub Issue


为了转换导入,您可以使用 Babel (jest docs)。

安装 babel:

yarn add --dev babel-jest @babel/core @babel/preset-env

通过创建 babel.config.js.babelrc

添加 babel 配置
module.exports = {
  presets: [['@babel/preset-env', {targets: {node: 'current'}}]],
};

working example on GitHub