You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用assert动态导入ES6 JSON模块时ESLint报错,如何解决?

问题描述

在Node.js 18.15环境中使用以下代码动态导入JSON文件:

await import(`${process.cwd()}/some.json`, { assert: { type: 'json' } });

ESLint 8.29.x报错“Parsing error: Unexpected token ,”;移除assert参数则触发Node.js报错TypeError [ERR_IMPORT_ASSERTION_TYPE_MISSING]。当前ESLint配置如下:

env:
  node: true
  es2021: true
  jest/globals: true
plugins:
  - jest
extends:
  - eslint:recommended
  - prettier
parserOptions:
  ecmaVersion: 2023
  sourceType: module
解决方法

修改ESLint配置中的env.node字段,明确指定Node.js版本为18,让ESLint识别该版本支持的导入断言语法即可:

env:
  node: '18' # 明确指定Node.js版本,开启对应语法支持
  es2021: true
  jest/globals: true
plugins:
  - jest
extends:
  - eslint:recommended
  - prettier
parserOptions:
  ecmaVersion: 2023
  sourceType: module

补充说明

导入断言是Node.js 17及以上版本支持的语法,ESLint默认的node: true不会自动启用高版本Node.js的特性,指定具体版本后,ESLint会加载对应版本的语法规则,不再报错。

内容的提问来源于stack exchange,提问作者MathKimRobin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 20:33:11