I'm working on a project which has a few different modules, for example:
/project
/app
/website
/api
/common
/dist (gets generated from the ts build)
/types (gets generated from the ts build)
/src
/index.ts
/security.ts
/accounting.ts
The common directory contains common functions/calculations etc. that I might need to access between the various other projects that way I don't need to duplicate code.
I am aware of npm link and have tried that, and works okay, but what I want is more fine grained imports, for example:
import accounting from 'common/accounting';
import security from 'common/security';
When I try this, I get the following error:
Cannot find module 'common/accounting' or its corresponding type declarations.ts(2307)
rather than doing these:
import common from 'common';
const accounting = common.accounting;
const security = common.security;
or this:
import accounting from 'common/dist/accounting';
import securityfrom 'common/dist/security';
(The above both work)
This is a list of things I have tried for package.json
for the common folder (there's probably more but I can't remember at the moment):
1
{
"name": "common",
"main": "dist",
"types": "types",
"exports": {
".": "./dist/index.js",
"./accounting": "./dist/accounting.js",
"./security": "./dist/security.js"
},
"dependencies": {...},
"scripts": {...}
}
2
{
"name": "common",
"main": "dist/index.js",
"types": "types/index.d.ts",
"dependencies": {...},
"scripts": {...}
}
3
{
"name": "common",
"main": "dist",
"types": "types/index.d.ts",
"dependencies": {...},
"scripts": {...}
}
4 (tsconfig.json)
{
"compilerOptions": {
"target": "es2017",
"module": "commonjs",
"outDir": "./dist",
"declaration": true,
"declarationDir": "./types",'.d.ts' files
"esModuleInterop": true,
"moduleResolution": "node",
"baseUrl": ".",
"typeRoots": ["./types"],
"paths": {
"common/*": ["../common/dist/*"]
}
},
"include": [
"./src/**/*" // source files to include in the compilation
]
}
Information:
- node: v18.14.2
- npm: v8.19.4
- IDE: vscode v1.80.0
- projects are NextJS except for API which is running Yoga server
EDIT: so the code appears to still work, BUT the IDE and typescript are saying it's an error