I am trying to share code between 2 seperate Typescript projects.
To demonstrate this easily reproducable problem I created a minimal repository here (Including instructions to reproduce):
https://github.com/realdegrees/ts-function-reference/tree/master/project
You can check out the repo or the following code snippets.
The issue is that I can import e.g. interfaces and enums just fine and they work (ran the code and had the enums logged) but as soon as it’s a function I’m trying to import it doesn’t work anymore.
Running tsc
works fine and throws no errors. You can even see that typescript resolves the module in question perfectly fine.
❯ tsc --traceResolution | grep 'reference/lib'
======== Resolving module '../reference/lib' from '/home/fasc/projects/reference-
test/project/index.ts'. ========
Loading module as file / folder, candidate module location
'/home/fasc/projects/reference-test/reference/lib', target file type 'TypeScript'.
File '/home/fasc/projects/reference-test/reference/lib.ts' exist - use it as a name
resolution result.
======== Module name '../reference/lib' was successfully resolved to
'/home/fasc/projects/reference-test/reference/lib.ts'. ========
======== Module name './lib' was successfully resolved to
'/home/fasc/projects/reference-test/reference/lib.ts'. ========
However as soon as I try to start the project it’s the ‘ole
Error: Cannot find module '../reference/lib'
I have tried importing the index.ts, lib.ts directly and paths in tsconfig.ts.
References are setup correctly and it obviously works with types but for some reason not functions.
The project looks like this: [Image][1]
Files:
////////////// project/index.ts
// None of these work
import { foo as regularDirectImport } from '../reference/lib';
import { foo as regularIndexImport } from '../reference/index';
import {foo as pathsImport} from '@reference';
pathsImport();
regularDirectImport();
regularIndexImport();
/////////// project/tsconfig.ts
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": ".",
"paths": {
"@reference": ["../reference/index"]
}
},
"references": [
{
"path": "../reference"
}
]
}
////////// reference/index.ts
export * from './lib';
////////// reference/lib.ts
export const foo = () => {
console.log('bar');
}
////////// reference/tsconfig.ts
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"outDir": "dist",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"baseUrl": "src",
"declaration": true,
"declarationMap": true,
"composite": true
}
}
I would die for some help, I have found several guides on how to import code from external typescript projects and they all mention that functions work as well but I just cannot get it work.
[1]: https://i.stack.imgur.com/KXOGc.png