A
arktype•15mo ago
OnkelTem

How to import `arktype` into a typescript project?

I do it like this:
import { type } from 'arktype';
import { type } from 'arktype';
However, I get an error:
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("arktype")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '....../package.json'.ts(1479)
The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic 'import("arktype")' call instead.
To convert this file to an ECMAScript module, change its file extension to '.mts', or add the field `"type": "module"` to '....../package.json'.ts(1479)
I tried to specify path to cjs version, but it didn't work:
import { type } from 'arktype/cjs'; // Cannot find module 'arktype/cjs' or its corresponding type declarations.ts(2307)
import { type } from 'arktype/cjs/main'; // Cannot find module 'arktype/cjs/main' or its corresponding type declarations.ts(2307)
import { type } from 'arktype/cjs'; // Cannot find module 'arktype/cjs' or its corresponding type declarations.ts(2307)
import { type } from 'arktype/cjs/main'; // Cannot find module 'arktype/cjs/main' or its corresponding type declarations.ts(2307)
How to get it imported?
15 Replies
OnkelTem
OnkelTemOP•15mo ago
Meh, tsconfig is the worst config I've seen I needed to correct this: "moduleResolution": "Node16" => "moduleResolution": "node" Nah, it didn't change anything. Or rather - it broke TypeScript So I don't have a solution right now on how to fix this issue I don't understand why TS cannot load arktype This is my current tsconfig.json
{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"resolveJsonModule": true,
"noErrorTruncation": true
}
}
{
"extends": "@tsconfig/node16/tsconfig.json",
"compilerOptions": {
"declaration": true,
"emitDeclarationOnly": true,
"resolveJsonModule": true,
"noErrorTruncation": true
}
}
The referenced @tsconfig/node16/tsconfig.json:
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 16",
"_version": "16.1.0",

"compilerOptions": {
"lib": ["es2021"],
"module": "node16",
"target": "es2021",

"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node16"
}
}
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "Node 16",
"_version": "16.1.0",

"compilerOptions": {
"lib": ["es2021"],
"module": "node16",
"target": "es2021",

"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"moduleResolution": "node16"
}
}
This is what seems to fix the issue:
"module": "ES2015",
"moduleResolution": "Bundler"
"module": "ES2015",
"moduleResolution": "Bundler"
becasue I use babel anyway
ssalbdivad
ssalbdivad•15mo ago
This is so weird uggh CJS/ESM stuff is the worst The package publishes build output for both CJS and ESM and I haven't seen anyone else having this issue so off the top of my head no idea what specifically is causing this to happen for you Also what is this why does package JSON have a million elipses before it 🤣
'....../package.json'.ts(1479)
Dimava
Dimava•15mo ago
@ssalbdivad could you please publish an "@typescript" branch on npm, especially for me? I don't really ever have an actual need to use cjs or mjs as I always have a bundler, so I make my packages TS-only (this would make sense to bun/deno users as well)
Dimava
Dimava•15mo ago
(There are points against this in general tho - https://gist.github.com/andrewbranch/6f11e6e0c3fb9a294590a061249264b0 )
Gist
tsc performance consuming .ts vs .d.ts files in npm packages
tsc performance consuming .ts vs .d.ts files in npm packages - tsc-ts-vs-dts.md
Dimava
Dimava•15mo ago
Tho I'd ask you to measure the perf
ssalbdivad
ssalbdivad•15mo ago
I used to have this for Deno. I've seen some of the .ts vs .d.ts stuff Can you just use the repo?
Dimava
Dimava•15mo ago
I dunno Can I pnpm install it?
ssalbdivad
ssalbdivad•15mo ago
Yeah you can point npm to a repo Obviously it's not efficient but 🤷
Dimava
Dimava•15mo ago
But can you do that for monorepos? Ackchually iirc there was a website to make those work
ssalbdivad
ssalbdivad•15mo ago
Hmm. I mean in alpha the main package was at the root level.
Dimava
Dimava•15mo ago
I mean beta, so.
ssalbdivad
ssalbdivad•15mo ago
There should be a straightforward-ish way to get it to work Or a bunch of ways that are mildly straightforward That don't involve me publishign something 😛 I wouldn't use that kind of dep for anything other than messing around though you don't want to install the website just to use a dependency
Dimava
Dimava•15mo ago
🤔 Having only es would be at least twice as simple as having cjs But I'm not sure if telling peoples "install at@cjs if you need that 💩" is good or not
ssalbdivad
ssalbdivad•15mo ago
That idea has crossed my mind I think the beta setup using tsup is fine though hopefully we'll see
OnkelTem
OnkelTemOP•15mo ago
it's just a <snip> 🙂
Want results from more Discord servers?
Add your server