'npm binary using ts-node causes errors when invoked using npm i -g or npx

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source