简体   繁体   English

如何获得 Docker 容器内部的自动绝对导入?

[英]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.我正在使用 VSCode 在第二台(远程)机器上处理 NodeJS 项目。 This project runs in a Docker container on that second machine.该项目在第二台机器上的 Docker 容器中运行。 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. Node 应用程序从不在远程机器上运行,它只在容器内运行,即使在开发期间也是如此。

Remote machine project location: /home/Tim/Docker/project/*远程机器项目位置: /home/Tim/Docker/project/*
Container project location: /app/*容器项目位置: /app/*
Example file EntityIDs.js :示例文件EntityIDs.js
-- Remote machine: /home/Tim/Docker/project/modules/data/EntityIDs.js -- 远程机器: /home/Tim/Docker/project/modules/data/EntityIDs.js
-- Container: /app/modules/data/EntityIDs.js -- 容器: /app/modules/data/EntityIDs.js

I tried setting the baseUrl in jsconfig.json to "."我尝试将baseUrl中的jsconfig.json设置为"." , but that results in an auto import of modules/data/EntityIDs.js , which won't work. ,但这会导致自动导入modules/data/EntityIDs.js ,这是行不通的。 It needs to be /app/modules/data/EntityIDs.js .它需要是/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.baseUrl设置为/app/会导致自动导入../home/Tim/Docker/project/modules/data/EntityIDs.js ,所以情况更糟。

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?所以, TL;DR :我怎样才能确保自动导入使用它将在其中运行的容器内部的绝对路径?

This feels like an absolute hack, and I don't like it at all, but it seems to work.这感觉就像一个绝对的 hack,我一点也不喜欢它,但它似乎有效。 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 .似乎将baseUrl设置为项目的根目录 ( ./project ),然后为所有内容添加/app/*路径确实为我提供了/app/modules/data/EntityIDs.js所需的自动导入路径。

Again, I'd love to know if this is a good solution or just a hack.同样,我很想知道这是一个好的解决方案还是只是一个 hack。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何连接到docker容器的localhost(从容器内部) - How do I connect to the localhost of a docker container (from inside the container) 自动在Docker容器中提取主机名的方法 - Automatic way to pick up the hostname inside docker container 如何从haproxy docker容器内的请求中获取客户端IP? - How to get client IP from request inside haproxy docker container? 如何在Docker中将数据从一个容器获取到另一个容器? 我收到ECONNREFUSED错误 - How can I get data from one container to another in docker? I am getting ECONNREFUSED error 我应该如何在Docker容器中将Thumbed作为服务运行? - How should I run thumbd as a service inside a Docker container? 我可以将另一个docker容器卷曲在docker内,但无法在代码中获取 - I can curl another docker container inside docker compose but not fetch in code 如何从另一个 docker 容器连接到作为 docker 容器启动的 Verdaccio 服务? - How can I connect to my Verdaccio service launched as docker container from another docker container? 如何在 Docker 中使用 process.argv 运行容器? - How can I run container with process.argv in Docker? 如何为我的 GraphQL docker 容器提供服务? - How can I serve my GraphQL docker container? 如何使用 docker-container 运行网络应用程序? - how can I run a web-app with a docker-container?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM