使用 Node.js 14.1.0 导入时出现错误“无法找到模块”

pat*_*njo 6 import node.js ecmascript-6 es6-modules

当尝试像这样导入时:

import { port } from './config';
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

错误 [ERR_MODULE_NOT_FOUND]:找不到模块“/Users/USER/PROJECT/src/config”

但是当我尝试这个导入时它工作正常:

import { port } from './config.js';
Run Code Online (Sandbox Code Playgroud)

config.js的内容:

export const port = 3000;
export const hostname = localhost;
Run Code Online (Sandbox Code Playgroud)

使用 Node.js v14.1.0 在我的 package.json 中将“type”设置为“module”

那么,如何使用无扩展名导入呢?

Shu*_*ari 1

编辑:

默认情况下使用此标志运行节点

node --experimental-specifier-resolution=node index.js
Run Code Online (Sandbox Code Playgroud)

在 config.js 的末尾,像这样导出模块:

node --experimental-specifier-resolution=node index.js
Run Code Online (Sandbox Code Playgroud)