Node.js + TypeScript2025/09/12 |
|
Configure TypeScript environment on Node.js. |
|
| [1] | |
| [2] | Create a test project with TypeScript as a common user. |
|
# generate package.json debian@dlp:~/testproject4$ npm init -y
Wrote to /home/debian/testproject4/package.json:
{
"name": "testproject4",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
# install TypeScript debian@dlp:~/testproject4$ npm install typescript ts-node @types/node added 20 packages, and audited 21 packages in 751ms found 0 vulnerabilities # create tsconfig.json
debian@dlp:~/testproject4$ cat > tsconfig.json <<EOF
{
"compilerOptions": {
"target": "esnext",
"module": "nodenext",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true
}
}
EOF
# create a test app debian@dlp:~/testproject4$ mkdir src
debian@dlp:~/testproject4$
vi src/test-app.ts
import * as http from "http";
import * as os from 'os';
const hostname = os.hostname();
const port = 3000;
const server = http.createServer((req, res) =>
{
res.write("Hello TypeScript World on Node.js!\n");
res.end();
}
);
server.listen(port);
console.log(`http://${hostname}:${port}`);
debian@dlp:~/testproject4$
vi package.json
"scripts": {
# change
"start": "ts-node src/test-app.ts"
debian@dlp:~/testproject4$ npm run start > testproject4@1.0.0 start > ts-node src/test-app.ts http://dlp.srv.world:3000 |
| Access to the URL that is shown on the console above from any client computer, and then that's OK if following app is shown. |
|
| Sponsored Link |