[英]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.