Running a Javascript file
Suppose you have a javascript file that you want to run from the command line.
- Install
node
ordeno
- Run the file with
node <filepath.js>
ordeno <filepath.js>
ordeno run <filepath.js>
Running a TypeScript file
Suppose you have a typescript file that you want to run from the command line.
- Install
node
.npm
comes bundled withnode
. - Install the typescript compiler with
npm install -g typescript
. Note that this will install the latest version of typescript compiler globally. - Compile the file with
tsc <filepath.ts>
- Run the file with
node <comiled-filepath.js>
- Alternatively, you can install the
ts-node
package withnpm install -g ts-node
. This will install the latest version ofts-node
globally.ts-node
compiles the file on the file and executes it.ts-node <filepath.ts>
Running a Javascript project
- The project directory must have a
package.json
file. This file is used bynpm
and can also be used to define additional information if you plan to publish the module tonpm
registry. - Install the dependencies mentioned in the
package.json
file withnpm install
, more details aboutpackage.json
- The
package.json
file have ascripts
section, this defines the commands that can be run withnpm run <command>
. In this section, you can define commands likestart: node index.js; test: jest .
, then if you runnpm run test
it will run the tests. - Note,
npm
defines three commands which can be run without using therun
command,npm start
,npm test
andnpm stop
. Thestart
command defaults to usingindex.js
file - The
package.json
file has amain
field, this is the file that is executed when you import the package in another file. - By default,
npm
treats the imports as CommonJS modules, if you are using ES modules, you need to add thetype
key in yourpackage.json
with valuemodule
. Then all files must useimport/export
syntax.
{
name: "my-package",
version: "1.0.0",
main: "index.js",
scripts: {
start: "node index.js",
test: "jest .",
},
type: "module",
dependencies: {
jest: "^27.5.1",
},
}
Running a TypeScript project
Using ts-node
[!NOTE] You can avoid using
ts-node
and usetsx
which supports TypeScript out of the box. No need to add additional support for esm modules
- In addition to the steps mentioned above for Javascript, the project directory must also have a
tsconfig.json
file. This file is used bytypescript
compiler to compile the files. Note you would still need to installtypescript
(orts-node
) 2. Thetsconfig.json
file has acompilerOptions
section, this defines the options for the compiler. In this section, you can define thetarget
option, which tells which version of ECMAScript to compile to (useESNext
for latest ECMAScript standard). There are more options available fortsconfig.json
, reference
{
compilerOptions: {
target: "ESNext", // which version of ECMAScript to compile to
module: "ESNext", // which module system to compile to (ECMAScript modules or CommonJS)
moduleResolution: "node", // resolve modules using Node.js style e.g. looking in `node_modules` folder
strict: true, // enable strict type checking
esModuleInterop: true, // enable interoperability with ES modules and CommonJS modules
skipLibCheck: true, // skip type checking of all declaration files (*.d.ts) these files might be used for third party modules types
},
include: ["src/**/*.ts"], // which files to include in compilation
exclude: ["node_modules", "dist"], // which files to exclude from compilation
outDir: "dist", // where to place the compiled files
// If you are using ECMAScript moduels and ts-node, then add the following
"ts-node": {
esm: true,
},
}
- In your
package.json
file, modify thescripts
section to include compilation commands for typescript files.
{
scripts: {
start: "node dist/index.js",
test: "jest .",
build: "tsc",
dev: "ts-node src/index.ts",
// dev: "ts-node --esm src/index.ts" // if you are using ES modules
},
}