繁体   English   中英

在 Node v14.14.0 中使用顶级 await 功能时,“SyntaxError:await 仅在异步函数中有效”

[英]'SyntaxError: await is only valid in async function' when using top level await feature in Node v14.14.0

我正在尝试使用 Node v14.8 中添加的新顶级 await。 请参阅此处此处了解更多信息。 我没有在这里找到有关使用此新功能的问题的问题,但我希望这不是重复的。 最接近的是@DanStarns 对这个问题的回答,但我也尝试过他的解决方案(他的回答是在 14.8 版本之前)。

  • 我知道 IIFE 过去一直是首选的解决方法之一,但这个问题与新的顶级等待功能有关,以及为什么尽管已随 Node 14.8 发布但仍会引发错误

我正在将我的 Axios 实例从我的控制器中提取到一个独立的服务中。 在该服务文件中,我尝试实例化然后导出使用 Spotify API 中存储的授权标头创建的 Axios 实例。 由于然后将其导入我的控制器(传递到构造函数然后它导出new SpotifyApiController(SpotifyApiService) )并且导入是同步的,我试图使用新的顶级 await 功能在启动/导入时完全实例化它,但得到以下错误:

[nodemon] starting `babel-node bin/www.js --harmony-top-level-await --experimental-repl-await`
E:\projects\harmonic-mixing-companion\server\services\SpotifyApiService.mjs:117
var SpotifyApiService = await createApiService();
                        ^^^^^

SyntaxError: await is only valid in async function
    at wrapSafe (internal/modules/cjs/loader.js:979:16)
    at Module._compile (internal/modules/cjs/loader.js:1027:27)
    at Module._compile (E:\projects\harmonic-mixing-companion\server\node_modules\pirates\lib\index.js:99:24)
    at Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
    at Object.newLoader [as .mjs] (E:\projects\harmonic-mixing-companion\server\node_modules\pirates\lib\index.js:104:7)
    at Module.load (internal/modules/cjs/loader.js:928:32)
    at Function.Module._load (internal/modules/cjs/loader.js:769:14)
    at Module.require (internal/modules/cjs/loader.js:952:19)
    at require (internal/modules/cjs/helpers.js:88:18)
    at Object.<anonymous> (E:\projects\harmonic-mixing-companion\server\controllers\/SpotifyApiController.js:1:1)

从错误来看,看起来 babel-node 正在正确地转换我的代码,因为const SpotifyApiService = ...变成了var SpotifyApiService = ... 我还在我的babel.config.json添加了"@babel/plugin-syntax-top-level-await"

该服务的文件扩展名为.mjs 我也尝试在我的服务器的 package.json 中设置"type": "module" ,但这也没有结果。 如果我错了,请纠正我,但将我的整个后端服务器设置为模块对我来说也不合适,因为对我来说它听起来不像是一个模块化单元(相对于可重用的SpotifyApiService )。

使用console.log(process.version);双重检查我的节点版本console.log(process.version); 在我的主条目文件的顶部,它打印了预期的 14.14.0 版本。


SpotifyApiController.js片段:

import SpotifyApiService from '../services/SpotifyApiService.mjs';
import handleHttpError from '../handlers/handleHttpError';

class SpotifyApiController {
  constructor(spotifyApiService) {
    this.spotifyApi = spotifyApiService;
  }
  ...
}

SpotifyApiService.mjs片段:

...
const createApiService = async () => {
  const accessToken = await authorizeApp();

  console.log(accessToken);

  const authHeader = { Authorization: `Bearer ${accessToken}` };

  const axiosInstance = axios.create({
    baseURL: 'https://api.spotify.com/v1',
    headers: authHeader,
  });

  createAuthRefreshInterceptor(axiosInstance, authorizeApp);

  return axiosInstance;
};

const SpotifyApiService = await createApiService();

export default SpotifyApiService;

package.json依赖项:

  "devDependencies": {
    "@babel/cli": "~7.12.1",
    "@babel/core": "~7.12.1",
    "@babel/eslint-parser": "~7.12.1",
    "@babel/node": "~7.12.1",
    "@babel/plugin-proposal-class-properties": "~7.12.1",
    "@babel/plugin-syntax-top-level-await": "~7.12.1",
    "@babel/plugin-transform-arrow-functions": "~7.12.1",
    "@babel/plugin-transform-runtime": "~7.12.1",
    "@babel/preset-env": "~7.12.1",
    "@babel/preset-react": "~7.12.1",
    "babel-loader": "~8.1.0",
    "nodemon": "~2.0.5"
  },
  "dependencies": {
    "axios": "~0.20.0",
    "axios-auth-refresh": "~3.0.0",
    "cookie-parser": "~1.4.5",
    "cors": "~2.8.5",
    "debug": "~4.2.0",
    "dotenv": "~8.2.0",
    "express": "~4.17.1",
    "querystring": "~0.2.0"
  }

服务器使用以下 npm 脚本启动: "dev-server": "nodemon --exec babel-node bin/www.js --ignore dist/"

您可以将其包装在 async 函数中:

var SpotifyApiService = (async()=> await createApiService())();

在你可以使用than 之后,catch

正如文档所示,babel 只为这个特性启用“语法”,

仅语法

此插件仅启用此功能的解析。 Babel 不支持转换顶级 await ,但您可以使用 Rollup 的 ExperimentalTopLevelAwait 或 webpack@5 的 Experiments.topLevelAwait 选项。

这只是意味着 babel 在编译你的代码时不会抱怨,但它实际上并没有实现会主动转换一行的步骤,比如var SpotifyApiService = await createApiService(); 进入有效的 ES5。

因此,执行您的代码(浏览器、节点)的任何内容都会抱怨,因为他们不知道如何处理此功能。

问题是顶级 await 仅在 ES 模块中受支持。 换句话说,您要么必须将"type": "module"到您的 package.json 或将您的.js文件重命名为.mjs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM