我正在用Typescript编写一个NodeJS (v18+)应用程序,它最终将被部署到Azure应用服务中。
我将其作为ECMAScript模块编写,因此我可以利用较新的功能,如顶层等待等。
使用下面的配置,我已经让我的应用程序构建和运行正确的独立与这些功能似乎没有问题,但是我正在努力能够调试与nodemon的动态。
我确信我的配置某处有问题,但我不知道什么
这是我的typescript.json:{
"compilerOptions": {
"target": "es2022", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */
"lib": ["ES2022"], /* Specify a set of bundled library declaration files that describe the target runtime environment. */
"module": "es2022", /* Specify what module code is generated. */
"rootDir": "src", /* Specify the root folder within your source files. */
"moduleResolution": "node", /* Specify how TypeScript looks up a file from a given module specifier. */
"resolveJsonModule": true, /* Enable importing .json files. */
"allowJs": false, /* Allow JavaScript files to be a part of your program. Use the 'checkJS' option to get errors from these files. */
"outDir": "build", /* Specify an output folder for all emitted files. */
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */
"strict": true, /* Enable all strict type-checking options. */
"noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
"skipLibCheck": true /* Skip type checking all .d.ts files. */
}
}
这是我的package.json
{
"type": "module",
"packageManager": "yarn@3.3.1",
"devDependencies": {
"@types/express": "^4.17.15",
"@types/fs-extra": "^11.0.1",
"@types/node": "^18.11.18",
"@typescript-eslint/eslint-plugin": "^5.48.2",
"@typescript-eslint/parser": "^5.48.2",
"@yarnpkg/sdks": "^3.0.0-rc.35",
"eslint": "^8.32.0",
"nodemon": "^2.0.20",
"rimraf": "^4.1.1",
"ts-node": "^10.9.1",
"typescript": "^4.9.4"
},
"scripts": {
"debug": "yarn nodemon",
"build": "yarn rimraf ./build && yarn tsc",
"start": "node build/app.js"
},
"dependencies": {
"axios": "^1.2.3",
"dotenv": "^16.0.3",
"express": "^4.18.2",
"fs-extra": "^11.1.0"
}
}
我的‘nodemon。Json ' '文件包含:
{
"watch": ["src"],
"ext": "ts,json",
"exec": "ts-node --inspect ./src/app.ts"
}
下面是我运行yarn debug
时的输出:
yarn nodemon --inspect src/app.ts
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node --inspect src/app.ts`
C:my-app.yarncachearg-npm-4.1.3-1748b966a8-544af8dd3f.zipnode_modulesargindex.js:88
const err = new Error(`Unknown or unexpected option: ${originalArgName}`);
^
Error: Unknown or unexpected option: --inspect
at arg (C:my-app.yarncachearg-npm-4.1.3-1748b966a8-544af8dd3f.zipnode_modulesargindex.js:88:19)
at parseArgv (C:my-app.yarn__virtual__ts-node-virtual-4e86653c50 cachets-node-npm-10.9.1-6c268be7f4-090adff130.zipnode_modulests-nodedistbin.js:69:12)
at main (C:my-app.yarn__virtual__ts-node-virtual-4e86653c50 cachets-node-npm-10.9.1-6c268be7f4-090adff130.zipnode_modulests-nodedistbin.js:25:18)
at Object.<anonymous> (C:my-app.yarn__virtual__ts-node-virtual-4e86653c50 cachets-node-npm-10.9.1-6c268be7f4-090adff130.zipnode_modulests-nodedistbin.js:579:5)
at Module._compile (node:internal/modules/cjs/loader:1218:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1272:10)
at require$$0.Module._extensions..js (C:my-app.pnp.cjs:13083:33)
at Module.load (node:internal/modules/cjs/loader:1081:32)
at require$$0.Module._load (C:my-app.pnp.cjs:12902:22)
at ModuleWrap.<anonymous> (node:internal/modules/esm/translators:169:29) {
code: 'ARG_UNKNOWN_OPTION'
}
Node.js v18.13.0
[nodemon] app crashed - waiting for file changes before starting...
如果我从nodemon.json
中的命令中删除--inspect
选项,我得到:
yarn nodemon
[nodemon] 2.0.20
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): src***
[nodemon] watching extensions: ts,json
[nodemon] starting `ts-node ./src/app.ts`
TypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".ts" for C:my-appsrcapp.ts
at new NodeError (node:internal/errors:400:5)
at Object.getFileProtocolModuleFormat [as file:] (node:internal/modules/esm/get_format:79:11)
at defaultGetFormat (node:internal/modules/esm/get_format:121:38)
at defaultLoad (node:internal/modules/esm/load:81:20)
at nextLoad (node:internal/modules/esm/loader:163:28)
at load$1 (file:///C:/my-app/.pnp.loader.mjs:1455:12)
at nextLoad (node:internal/modules/esm/loader:163:28)
at ESMLoader.load (node:internal/modules/esm/loader:605:26)
at ESMLoader.moduleProvider (node:internal/modules/esm/loader:457:22)
at new ModuleJob (node:internal/modules/esm/module_job:64:26) {
code: 'ERR_UNKNOWN_FILE_EXTENSION'
}
[nodemon] app crashed - waiting for file changes before starting...
这是我的VSCode启动。json文件-虽然现在我只是试图让nodemon在命令行上工作,然后再考虑将其放入编辑器。
"version": "0.2.0",
"configurations": [
{
"name": "Debug via Nodemon",
"request": "launch",
"runtimeArgs": [ "debug" ],
"runtimeExecutable": "yarn",
"skipFiles": [
"<node_internals>/**"
],
"type": "node",
"console": "integratedTerminal"
}
]
}
这种配置以前对我没有问题,但只适用于commonjs项目。
我一直在尝试各种不同的ts-node/nodemon参数的组合来让这个工作,但我似乎没有多大的成功。大多数在线解决方案似乎是从软件包中删除type: module
选项。让它成为commonjs模块,但这里我不想这么做。我只是希望能够在vscode中进行调试,当源文件发生变化时,无需重新编译。
所以在网上搜索后,我修改了我的nodemon.json
文件来读取
{
"watch": ["src"],
"ext": ".ts,.js",
"ignore": [],
"exec": "node --loader ts-node/esm ./src/app.ts"
}
并将package.json
中的debug
脚本更改为yarn nodemon
。
我然后有ts-node
找不到的问题,尽管已经安装。最后我使纱线的塞子失效了。通过添加.yarnrc.yml
文件来发挥功能,该文件读取:nodeLinker: node-modules
,删除我现有的.yarn
文件夹,任何.pnp.
文件并重新运行yarn
,这似乎已经修复了事情。看起来这和yarn的Plug &玩。