ESM无法使用index.js从文件夹导入所有文件 [英] ESM importing all files from folder with index.js not working

查看:94
本文介绍了ESM无法使用index.js从文件夹导入所有文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑这个非常简单的项目: esm测试

Node.js v13 上运行.

该项目的 package.json 中具有"type":"module" ,因此Node.js将默认处理所有文件 .js 作为ECMAScript模块.请参阅: ECMAScript模块-启用.

它具有以下非常简单的结构:

 -package.json-src/- 东西/-a.js-b.js-index.js-app.js 

文件 app.js 是程序包的入口点.

我的目的是使用 index.js 文件从 stuff/文件夹导入所有文件.我知道在ES6中,文件夹中有一个 index.js 文件,您可以隐式地从该文件夹执行导入,而无需在import语句中指定 index.js ./p>

我可以这样做:

 从'./stuff'导入{a,b} 

代替:

 从'./stuff/a.js'导入一个从'./stuff/b.js'导入b 

但这对我不起作用.它显示以下错误:

  internal/modules/esm/default_resolve.js:96让url = moduleWrapResolve(specifier,parentURL);^错误:找不到从C:\ projects \ esm-test \ src \ app.js导入的模块C:\ projects \ esm-test \ src \ stuff在Loader.resolve [作为_resolve](内部/模块/esm/default_resolve.js:96:13)在Loader.resolve(内部/模块/esm/loader.js:72:33)在Loader.getModuleJob(内部/模块/esm/loader.js:156:40)在ModuleWrap中.(内部/模块/esm/module_job.js:42:40)链接(internal/modules/esm/module_job.js:41:36){代码:"ERR_MODULE_NOT_FOUND"} 

与CommonJS模块一起使用.但是我想用ESM做到这一点.

您能帮我吗?

解决方案

在esm中自动导入index.js是node.js当前版本中的一项实验性功能.您可以在启动服务器时使用标志 -experimental-specifier-resolution = node .

EX- node --experimental-specifier-resolution = node index

请参考此链接以获取更多信息 https://nodejs.org/api/esm.html#esm_resolver_algorithm

Consider this very simple project: esm-test

Runs on Node.js v13.

This project has "type":"module" in his package.json so Node.js will treat by default all files .js as ECMAScript modules. See: ECMAScript Modules - Enabling.

It has the following very simple structure:

- package.json
- src/
  - stuff/
    - a.js
    - b.js
    - index.js
  - app.js

The file app.js is the package entry point.

My purpose is to import all files from stuff/ folder using the index.js file. I know that in ES6, having an index.js file in a folder lets you perform an import from the folder implicitly without specifying the index.js in the import statement.

So I can do:

import { a, b } from './stuff'

Instead of:

import a from './stuff/a.js'
import b from './stuff/b.js'

But it's not working for me. It shows the following error:

internal/modules/esm/default_resolve.js:96
  let url = moduleWrapResolve(specifier, parentURL);
            ^

Error: Cannot find module C:\projects\esm-test\src\stuff imported from C:\projects\esm-test\src\app.js
    at Loader.resolve [as _resolve] (internal/modules/esm/default_resolve.js:96:13)
    at Loader.resolve (internal/modules/esm/loader.js:72:33)
    at Loader.getModuleJob (internal/modules/esm/loader.js:156:40)
    at ModuleWrap.<anonymous> (internal/modules/esm/module_job.js:42:40)
    at link (internal/modules/esm/module_job.js:41:36) {
  code: 'ERR_MODULE_NOT_FOUND'
}

With CommonJS modules works. But I want to do it with ESM.

Can you help me with this please.

解决方案

Import index.js automatically in esm is an experimental feature in the current version in node.js. You can use flag --experimental-specifier-resolution=node while staring server.

EX- node --experimental-specifier-resolution=node index

Plz refer this link for more info https://nodejs.org/api/esm.html#esm_resolver_algorithm

这篇关于ESM无法使用index.js从文件夹导入所有文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆