我正在尝试使用在Node v14.8中添加的新的顶层等待功能。
请查看此处和此处 获取更多信息。我没有在这里找到一个关于使用这个新功能时出现问题的问题,但我希望这不是重复的。最接近的是@DanStarns在这个问题上的答案,但我也尝试过他的解决方案(而且他的答案是在14.8发布之前的)。
- 我知道过去IIFE一直是解决方法之一,但这个问题涉及到新的顶层等待功能,以及为什么尽管已经随着Node 14.8的发布而释放了,却会抛出错误
我正在将我的Axios实例从控制器中提取到一个独立的服务中。在那个服务文件中,我试图使用存储的Spotify API授权标头创建一个Axios实例并导出它。由于这是在我的控制器中导入的(通过将其传递给构造函数,然后导出new SpotifyApiController(SpotifyApiService)
),并且导入是同步的,因此我正在尝试使用新的顶层等待功能,在启动/导入时完全实例化它,但是出现以下错误:
[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);
双重检查了我的node版本,输出了预期的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/"