简体   繁体   中英

How can I get automatic absolute imports for inside a Docker container?

I'm using VSCode to work on a NodeJS project on a second (remote) machine. This project runs in a Docker container on that second machine. I don't like relative imports, so I try to avoid them. I do like automatic imports, so I try to use them as much as possible.

The Node app never runs on the remote machine, it only runs inside the container, even during development.

Remote machine project location: /home/Tim/Docker/project/*
Container project location: /app/*
Example file EntityIDs.js :
-- Remote machine: /home/Tim/Docker/project/modules/data/EntityIDs.js
-- Container: /app/modules/data/EntityIDs.js

I tried setting the baseUrl in jsconfig.json to "." , but that results in an auto import of modules/data/EntityIDs.js , which won't work. It needs to be /app/modules/data/EntityIDs.js . Setting baseUrl to /app/ resulted in an auto import of ../home/Tim/Docker/project/modules/data/EntityIDs.js , so that's even worse.

I searched all over, and I can't imagine I'm the only one ever to run into this problem, but my search results suggest otherwise. Either I'm all alone with my problem (unlikely), or I just really don't know what to search for (a lot more likely)

So, TL;DR : How can I make sure that an automatic import uses the absolute path from inside the container it will be running in?

This feels like an absolute hack, and I don't like it at all, but it seems to work. PLEASE, if you know anything about this, let me know if this is an acceptable way of doing this, or if it's really just a workaround.

jsconfig.json

{
    "compilerOptions": {
        ...
        "baseUrl": "./",
        "paths": {
            "/app/*": ["*"]
        }
    },
    ...
}

It seems like setting the baseUrl to the root of the project ( ./project ), and then adding a path of /app/* for everything does give me the desired auto import path of /app/modules/data/EntityIDs.js .

Again, I'd love to know if this is a good solution or just a hack.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM