#TS2307: Cannot find module '~shared/types' or its corresponding type declarations.

7 messages · Page 1 of 1 (latest)

sharp rover
#

I've got a project setup where I have a root tsconfig and tsconfigs for each "sub module" where I'm using aliases in each one. ~/ points to that modules dir, and ~shared points to a src/shared module. My tsconfigs seem to be correct and I can build my code with rollup and run it, but I get an error during that processs.

(!) Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module '~shared/utils' or its corresponding type declarations.
src/backend/index.ts: (2:32)

2 import { createResponse } from '~shared/utils';

I know thats coming from the rollup ts plugin, but I can't figure out why.

Run: npm run test:shared
https://codesandbox.io/p/devbox/dy9mvn

turbid dust
#

!:paths*

normal orchidBOT
#
tjjfvi#0
`!t6:paths-are-not-magic`:

The paths and baseUrl compiler options don't cause any remapping of imports paths, they only inform TS of existing mappings, which you'll have to setup with some other tool.

baseUrl is a pretty well-supported option (e.g. using the NODE_PATH environment variable with node or resolve.modules with webpack).
paths can be trickier to setup, (especially with node see this for node), and you may find it to not be worth the effort.

turbid dust
#

@sharp rover the ~shared you've defined in the paths of your tsconfig.json is only used by the compiler to check your types, it is not used by other tools

#

also, those paths are not transformed, the paths you writes are the paths ou get in the emitted JS, valid or not

sharp rover
#

Right, but I have those paths defined in my rollup config as well to be replaced and the ts plugin loads the tsconfig which I would think would include those paths. I've put the paths a few different places trying to figure it out

#
import alias from '@rollup/plugin-alias';
import commonjs from '@rollup/plugin-commonjs';
import json from '@rollup/plugin-json';
import resolve from '@rollup/plugin-node-resolve';
import typescript from '@rollup/plugin-typescript';
import { fileURLToPath } from 'url';
import path from 'path';

const __dirname = path.dirname(fileURLToPath(import.meta.url));

export default {
  input: {
    'index': 'src/backend/index.ts',
    'test-shared': 'src/backend/test-shared.ts'
  },
  output: {
    dir: 'dist/backend',
    format: 'esm',
    sourcemap: true
  },
  external: [
    /node_modules/
  ],
  plugins: [
    alias({
      entries: [
        { find: /^~shared\/(.*)$/, replacement: path.resolve(__dirname, 'src/shared/$1') },
        { find: /^~\/(.*)$/, replacement: path.resolve(__dirname, 'src/backend/$1') }
      ]
    }),
    resolve({ 
      preferBuiltins: true,
      extensions: ['.ts', '.js', '.json']
    }),
    commonjs(),
    json(),
    typescript({ 
      tsconfig: path.resolve(__dirname, 'tsconfig.json'),
      include: [
        'src/backend/**/*.ts',
        'src/shared/**/*.ts'
      ],
      noEmitOnError: false,
      sourceMap: true,
      inlineSources: true
    })
  ]
};