Typescript in Docker: "Cannot find module" in Visual Studio code host typescript compiler

Project setup

I have a Typescript project I’m running in a local Docker container. I have the following two volumes (this is in docker-compose.yml):

volumes:
  - .:/node/app

This first volume is a bind mount, to copy in my app’s source code into the container.

  - /node/app/node_modules/

This second volume is inside the bind mount. This means that when an npm install runs inside the container, in the project’s /node/app working directory, the dependencies will be installed into the node_modules folder in the container only.

My understanding is this is a common practice to allow the host and the container to have separate node_modules. My intention for this setup is to install and run some tools in the host only, like prettier, so I can npm install --only=development in the host, and get prettier installed, as its specified as a devDependency, and now my editor can use prettier.

The problem

In my editor, when the typescript compiler runs, it shows me “Cannot find module” errors:

cannot find module errors in visual studio code

My understanding is this happens because those modules aren’t on disk. This is because of my above setup. I have made the node_modules containing my non-dev dependencies, like React, only available inside the container.

My question

I’m new to Docker, and my understanding is it’s generally good to do things inside the container, including development. I’m trying to avoid an npm install in the host, because I don’t really want or need all of the dependencies in the host. I also don’t want to have to npm install twice, once on host and once in container, when adding a new dependency.

Is there a way to stop Typescript from erroring if the node_modules imported aren’t present (like only load their typing definitions, maye?), or is there a way to point Typescript’s compiler in Visual Studio Code somehow to the container, so it can see them without having to install them on the host, while still letting me install other dependencies on the host, like prettier?

Or is the only way to npm install on the host?

Source: StackOverflow