节点 20 - 不带 .js 文件扩展名的 ESM 导入不起作用

问题描述 投票:0回答:1

我创建了一个干净的节点项目:

我正在运行命令:

node --experimental-specifier-resolution=node index.js

在index.js 中,我导入不带.js 文件扩展名的sayHello。这应该有效,因为我有experimental-specifier-resolution=node,并且在package.json中也有type:module。

package.json 是:

{
  "name": "test",
  "engines": {
    "node": "20.10.0"
  },
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "author": "",
  "license": "ISC"
}

sayHello.js 是:

export function sayHello() {
    console.log('hello');
}

index.js 是:

import { sayHello } from "./sayHello";

sayHello()
console.log(process.version);

此外,使用 Node 16 时相同的配置也可以正常工作。知道缺少什么吗?

node.js es6-modules
1个回答
0
投票

看起来以智能方式手动重命名将是一个解决方案: https://stackoverflow.com/a/62626938/412992

并更新。

© www.soinside.com 2019 - 2024. All rights reserved.