I have a simple project
$ ls -l
total 32
-rw-rw-r-- 1 ocket8888 ocket8888 72 Apr 29 09:30 index.ts
-rw-rw-r-- 1 ocket8888 ocket8888 105 Apr 29 09:31 main.ts
drwxrwxr-x 4 ocket8888 ocket8888 4096 Apr 29 09:26 node_modules
-rw-rw-r-- 1 ocket8888 ocket8888 206 Apr 29 09:27 package.json
-rw-rw-r-- 1 ocket8888 ocket8888 1000 Apr 29 09:26 package-lock.json
-rw-rw-r-- 1 ocket8888 ocket8888 222 Apr 29 09:33 tsconfig.json
In index.ts
I export a class, and in main.ts
I import that class.
index.ts
export class Testquest {
constructor(public readonly foo: string) {}
}
main.ts
#!/usr/bin/env node
import { Testquest } from ".";
const a = new Testquest("bar");
console.log(a.foo);
When I try to build this, tsc
says:
$ npx tsc
main.ts:2:10 - error TS2459: Module '"."' declares 'Testquest' locally, but it is not exported.
2 import { Testquest } from ".";
~~~~~~~~~
main.ts:2:10
2 import { Testquest } from ".";
~~~~~~~~~
'Testquest' is declared here.
Found 1 error in main.ts:2
As you can see, however, the class is exported. So I don't know why Typescript is so sure it isn't.
package.json
{
"name": "ts-export-test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"type": "module",
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^4.6.4"
}
}
tsconfig.json
{
"compilerOptions": {
"target": "es2016",
"module": "ES6",
"moduleResolution": "node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}
Note that importing from the non-existent file ./index.js
works, but my linter tells me that the /index.js
portion is a "useless path segment", and I know from experience I should be able to import symbols from a directory if it has an index.ts
file that exports the symbol - I don't know why that's suddenly not working here.
CodePudding user response:
You are currently read the main.ts
with node
. Replace the first line with ts-node
, to use TypeScript
to read it.
Here are 3 ways to handle the error.
1. Remove the first line
The first Shebang is not needed, TS can resolve and handle the file itself.
2. Replace with ts-node
main.ts
#!/usr/bin/env ts-node
3. Another way
Also if you don't want to install ts-node globally: Github: ts-node #73
#!/usr/bin/env -S node -r "ts-node/register"
Please make sure you have install ts-node correctly.
References
GitHub: ts-node
Is there a ts-node hashbang? #298
Executables without extension #116
CodePudding user response:
Change type of module to commonjs in your package.json
and tsconfig.json
In package.json
change "type": "commonjs",
{
"name": "ts-export-test",
"version": "1.0.0",
"description": "",
"main": "main.js",
"type": "commonjs",
"author": "",
"license": "ISC",
"devDependencies": {
"typescript": "^4.6.4"
}
}
in tsconfig.json
change "module": "commonjs",
remove "moduleResolution": "node",
{
"compilerOptions": {
"target": "es2016",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
}
}