头图

ts-node is the TypeScript execution engine and REPL of Node.js.

It converts TypeScript to JavaScript by JIT, allowing you to execute TypeScript directly on Node.js without pre-compilation. This is achieved by hooking into Node's module loading API, which enables it to be used seamlessly with other Node.js tools and libraries.

Note that in the windows operating system, if you run ts-node directly, an error will be reported:

'ts-node' is not recognized as an internal or external command,
operable program or batch file.

Many people suggest to install ts-node globally to solve this problem, but global installation is not a good solution.

Refer to this StackOverflow discuss .

I don’t recommend relying on the globally installed ts-node in your own modules, because if you do, then anyone who installs your module will need to install ts-node globally (just a normal npm install is not enough) then If two modules need to install things like ts-node globally, you may encounter version conflicts.

To avoid this, all your dependencies should be defined in package.json and installed locally in node_modules.

There is a little-known command npx for running binaries from modules installed locally in node_modules.

In SAP Spartacus, ts-node is used to directly execute .ts files in the project. An example is as follows:

loadtest:ssr, defined in the following file:

Even if I execute yarn loadtest:ssr in the root directory, I can directly start the script defined in package.json in the nested core-libs directory.

It turned out that a script with the same name was added under package.json in the root directory:

More original articles by Jerry, all in: "Wang Zixi":


注销
1k 声望1.6k 粉丝

invalid