金键盘
发布于 2025-07-10 / 2 阅读
0
0

TypeScript + NodeJS控制台命令行应用,如何做到index.ts适配有ES6模块或者无ES模块

tsconfig.json 用umd打包方法

头两行可以选择注释掉就是无模块。用umd都可以顺利编译、运行

import { myutil } from "./myutil";

myutil.exportIt();

let now = new Date(Date.now() + 8 * 60 * 60 * 1000);
let time = now.toISOString();
time = time.substring(0, time.length - 5);
time = time.replace("T", " ");
console.log(time);

tsconfig.json

{
    "compilerOptions": {
        "target": "es5",
        "module": "umd",
        "lib": [
            "dom",
            "es2015"
        ],
        "inlineSourceMap": true,
        "outDir": "./js",

tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "watch一下",
            "type": "typescript",
            "tsconfig": "tsconfig.json",
            "option": "watch",
            "problemMatcher": [
                "$tsc-watch"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Launch Program",
            "program": "${workspaceFolder}/js/index.js"
        }
    ]
}


评论