Getting `Unknown file extension ".ts"` error
Closed this issue · 5 comments
Steps to reproduce
- Create a simple Node.js project
- Install Pinion as a development dependency
- Install Typescript as a development dependency
- Use the basic example given in the quick start guide to create a code generator
- Use the command
npx pinion path/to/file.ts
to execute the generator created in the previous section
You will get the error below:
Oh no! Something went wrong: Unknown file extension ".ts" for path/to/file.ts
It appears to work fine when I rewrite the generator in JavaScript.
I tried debugging and the error seems to occur in the file below.
https://github.com/featherscloud/pinion/blob/main/packages/pinion/src/utils.ts#L36
Looks like the tsx
module is being imported with .ts
files but I'm not sure how importing the module is supposed to help.
https://github.com/featherscloud/pinion/blob/main/packages/pinion/src/utils.ts#L33
Apparently, import
ing a .ts
file as in the example below will throw the "Unknown file extension" error. It only works when you use tsx from the command line like so: npx tsx index.ts
.
import { dirname, join } from "path";
import { fileURLToPath } from "url";
const __dirname = dirname(fileURLToPath(import.meta.url));
const fileName = join(__dirname, "test.ts");
console.log(fileName);
import(fileName)
.then((response) => {
console.log(response);
})
.catch((reason) => {
console.error(reason);
});
Expected behavior
The generator should be able to create a readme.md
file as described in the docs.
Actual behavior
You will get the error below instead.
Oh no! Something went wrong: Unknown file extension ".ts" for path/to/file.ts
System configuration
I've set the package.josn's type
field to module
and installed Typescript as a development dependency.
Module versions (especially the part that's not working):
@featherscloud/pinion
version 0.5.3
NodeJS version:
18.15.0
Operating System:
Pop!_OS
Browser Version:
N/A
React Native Version:
N/A
Module Loader:
ECMAScript module loader
Importing the TSX module should initialise the TypeScript module loader. I just double checked with the following package.json
:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"@featherscloud/pinion": "^0.5.3"
},
"devDependencies": {
"typescript": "^5.4.4"
}
}
On Node v20.10.0 on Mac OS and it appears to run as expected if I put the example file in generate/readme.ts
and then run npx pinion generate/readme.ts
. The CI tests are running on the latest Ubuntu (with Node 18 and 20) testing the same thing are passing, too. Do you have a repository to reproduce the error?
Thanks for the quick response @daffl.
I tried again with Node v20.12.1 and it works. The issue is probably with versions of Node below 20. It would be useful to include the required Node version in the docs.
You can check out using this repo.
https://github.com/nibble0101/pinion-demo
You're right, I can confirm this. Interesting that CI is passing with Node 18. I'll update the requirements to Node 20
Alright, I updated the dependency requirement so it should throw an error (or at least warning) if you are using an older version. Thank you for reporting this.
Alright, I updated the dependency requirement so it should throw an error (or at least warning) if you are using an older version. Thank you for reporting this.
Thank you.