0

How to run a Typescript ... script directly, including imports from NPM, without the need for a package.json?

Suppose I want to write a quick script to alter a bunch of files quickly e.g.:

// my-script.ts

import fs from "fs";
import { someFunc } from "some-dep@^4";  // dep pulled from NPM during run

fs.readdir(process.argv[2], (err, files) => {
  files.forEach(file => {
    someFunc(file);
  });
});

I'd like to run this easily, from a terminal, such as:

<cmd> my-script

Where <cmd> is whatever program allows me to do this. Does this mystery program exist?

TL;DR: I want to be able to run TS files (with NPM modules) as if they were bash scripts.

4
  • Does this answer your question? How to run TypeScript files from command line? Commented Oct 4, 2023 at 17:23
  • I don't think so, because it would require a package.json and running npm install. I just want to write and run the script, and have modules be installed/handled for me. Along with ts-node, there is also esrun which do similar things but I don't think either "solves" what I'm asking. Commented Oct 4, 2023 at 19:05
  • 1
    If you want to include a package without having a package.json, use Deno. It allows inclusions in the manner of your example Commented Oct 4, 2023 at 19:15
  • 1
    Does this answer your question? How to use npm module in DENO? Commented Oct 5, 2023 at 3:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.