I have a module which exposes a bin in package.json which points to a TypeScript file. The .ts file has a ts-node shebang and is executable and works well.
However, when trying to use this binary from another package, I get errors:
Using npm i -g to install globally, when the binary is called which in turn calls ts-node I get errors for all the missing types declared in devDependencies of the module containing the binary, since devDependencies aren't installed when installing the module from another module. I have to manually install all the devDependencies such as `npm i -g @types/lodash" which makes no sense to do.
Using npx causes module related errors since I'm trying to use import syntax etc from a stand-alone ts-node call:
(node:67861) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
SyntaxError: Cannot use import statement outside a module
Is there any way to build a module which exposes a binary which is a wrapper around a ts-node script, and where that binary is actually executable through npm i -g or npx?
"dependencies".NPM says that "devDependencies" is for "Packages that are only needed for local development and testing" while "dependencies" is for "Packages required by your application in production". So what is "production"? In the case of modules like this, "production" means use in other packages!
In other words, since the type-only packages are still truly required for use by a consumer, they don't belong in "devDependencies" but in "dependencies". This wouldn't be the case if you were pre-compiling into plain JS, but since you're not, the type packages simply need to be there in "production".
Note that this won't necessarily increase your ultimate bundle size in e.g. a web app. If your consumer package lists your tool package in "devDependencies", then ultimately the types packages will not be considered necessary for "production" for the consumer package.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With