gpt4 book ai didi

node.js - 无法让 ava transpile 处理相对导入的文件

转载 作者:行者123 更新时间:2023-11-28 20:50:19 25 4
gpt4 key购买 nike

ES6 导入在此文件中有效,但在导入相关文件(例如我的 Mongoose 用户模型)时生成意外标记 import 错误。

import mongoose from 'mongoose';
^^^^^^

SyntaxError: Unexpected token import

.babelrc

{
"presets": [
["es2015", { "modules": false }]
],
"plugins": [
"transform-object-rest-spread",
"transform-async-to-generator",
"transform-export-extensions"
]
}

package.json

 "ava": {
"require": [
"babel-register"
]
}

users.test.js

import test from 'ava'
import axios from 'axios'

import User from '../../models/user'
import { USER_REGISTRATION_ROUTES } from '../helpers/_test.properties.js'

test('user registration api works', async function (t) {
const email = `test${Date.now()}@example.com`

const userRegistrationData = {
email,
first_name: "john",
last_name: "doe",
password: "password"
}

await axios.post(USER_REGISTRATION_ROUTES, userRegistrationData)
.then(response => {
const data = response.data.data
const user = data.user
t.deepEqual(response.status, 200, 'Verify: response is successful')
t.deepEqual(data.registered, true, 'Verify: user is registered')
t.truthy(Object.keys(user).length > 0,
'Verify: if registered, user object is returned')
t.truthy(user.authentication_token,
'Verify: token is generated on successful registration')
})
.catch((err) => {
t.fail(`Cannot make requst to register user ${err}`)
})

User.remove({ email }, function (err) {
if (err) {
console.log('error')
} else {
console.log('success deleting test user')
}
})
})

最佳答案

不要认为 nodejs 还支持 import。您必须使用 require

const mongoose = require('mongoose');

关于node.js - 无法让 ava transpile 处理相对导入的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49800285/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com