如何在 Docker 容器内获得自动绝对导入?
我正在使用 VSCode 在第二台(远程)机器上处理 NodeJS 项目。该项目在第二台机器上的 Docker 容器中运行。我不喜欢相对进口,所以我尽量避免它们。我确实喜欢自动导入,所以我尝试尽可能多地使用它们。
Node 应用程序从不在远程计算机上运行,它仅在容器内运行,即使在开发期间也是如此。
远程机器项目位置:/home/Tim/Docker/project/*
容器项目位置:/app/*
示例文件EntityIDs.js
:
-- 远程机器:/home/Tim/Docker/project/modules/data/EntityIDs.js
-- 容器:/app/modules/data/EntityIDs.js
我尝试将 jsconfig.json
中的 baseUrl
设置为 ”。 "
,但这会导致自动导入 modules/data/EntityIDs.js
,这是行不通的。它需要是 /app/modules/data/EntityIDs.js
。将 baseUrl
设置为 /app/
会导致自动导入 ../home/Tim/Docker/project/modules/data/EntityIDs.js
,所以情况更糟。
我到处搜索,我无法想象我是唯一遇到这个问题的人,但我的搜索结果表明并非如此。要么我独自面对我的问题(不太可能),要么我真的不知道要搜索什么(更有可能)
所以,TL;DR:我怎样才能确保自动导入使用它将运行的容器内部的绝对路径?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这感觉绝对是一种黑客行为,我一点也不喜欢它,但它似乎有效。请,如果您对此有所了解,请告诉我这是否是一种可以接受的方法,或者这实际上只是一种解决方法。
jsconfig.json
似乎将
baseUrl
设置为项目的根目录(./project
),然后添加路径>/app/*
对于所有内容确实给了我所需的/app/modules/data/EntityIDs.js
自动导入路径。再次,我很想知道这是否是一个好的解决方案或只是一个黑客。
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
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.