0

I would like to know how to import from the barrel file by referencing the directory and not the index file & extension. I don't want to use any .json file to get it.

In my case I have like 10 imports from that directory, and I would not like to see 10 lines of imports in the file that needs them, so I made an index.ts file to export these 10 modules or functions, and when trying to import it as:

import { foo1, foo2, foo3, foo4 } from '../foo';

I get the following error:

Unable to load a local module: "file:///home/iuser/project/src/foo". Please check the file path

It works by putting the file as follows:

import { foo1, foo2, foo3, foo4 } from '../foo/index.ts';

I will try to be clearer with the following example:

My models directory:

models

User.ts

Task.ts

Server.ts

Game.ts

Paris.ts

index.ts

index.ts

export * from 'User.ts';
export * from 'Task.ts';
export * from 'Server.ts';
export * from 'Game.ts';
export * from 'Paris.ts';

This is what I'm looking for: joinController.ts

import { User, Task, Server, Game, Paris } from '../models';
...

And what I don't want to do is reference the index.ts:

import { User, Task, Server, Game, Paris } from '../models/index.ts';
...

But honestly it doesn't seem clean code to me. I'm used to Node & ES6.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
cacascacas
  • 13
  • 4
  • Deno follows the ES module spec by resolving import specifiers literally. This behavior is not configurable via loaders as it is with Node.js. See the relevant manual section [Using Import Maps](https://deno.land/manual@v1.29.4/node/import_maps). – jsejcksn Jan 21 '23 at 22:13
  • Thank you very much for the suggestion, I did not know about this topic until after reading it, however it is not what I am looking for, I do not want to keep it as the node modules, before I did not specify that what I am looking for is to export classes, functions, etc. , instead I mentioned the word modules, which is not correct, now that I added another broader example, perhaps it will be better understood. – cacascacas Jan 22 '23 at 02:42

0 Answers0