A
A
alex4answ2020-05-28 07:26:01
Node.js
alex4answ, 2020-05-28 07:26:01

Absolute import in Node.js?

Good afternoon, I'm trying to do "absolute imports" (from the application root src)
i.e. instead of relative ones:
'../../../model/User'
I want to import like this (from anywhere):
'model/User'

Now for absolute imports I use the node_modules directory, i.e. my structure is like this:

node_modules
src
- node_modules
-  - models // тут User который пытаюсь импортировать
-  - остальные файлы/директории
package.json


I tried to set NODE_PATH=./src (both the full path and what options I just didn't try), the variable is simply ignored. (but in process.env.NODE_PATH - the value that I set is present)

Redefine the standard require or create its analogue and name it somehow include - I don't like this approach, why? - I can’t say for sure, the feeling that I’m wet in the insides of the node Tell me

what options there are, and why NODE_PATH is ignored (although the documentation says what else is supported)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
casoer, 2020-05-28
@casoer

in package.json dependencies do "model": "./src/node_modules/models"

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question