用于构建打字稿的 NPM 脚本不起作用
Posted
技术标签:
【中文标题】用于构建打字稿的 NPM 脚本不起作用【英文标题】:NPM script to build typescript not working 【发布时间】:2021-08-21 15:58:24 【问题描述】:[更新]
这个问题最初是通过使用npm uninstall tsc
卸载tsc
解决的(正如标记答案所建议的那样)。但是,问题又回来了,经过很长时间后,我尝试将 Windows Server for Linux (WSL) 与 VSCode 一起使用。它立即成功了,所以这就是我一直坚持的解决方案。
我在使用 NPM 脚本运行 tsc
命令时遇到问题:
"scripts":
"build": "tsc"
,
在 Windows 命令终端中运行 tsc
运行良好,但使用 NPM 脚本运行 tsc
却不行。 当我在 NPM 脚本中运行 tsc
时,我得到以下内容命令控制台:
> npm run build
> skill-tree@1.0.0 build
> tsc
'D\skill-tree\node_modules\.bin\' is not recognized as an internal or external command,
operable program or batch file.
node:internal/modules/cjs/loader:928
throw err;
^
Error: Cannot find module 'D:\Documents\~College\~Coding\tsc\bin\tsc'
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:925:15)
at Function.Module._load (node:internal/modules/cjs/loader:769:27)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47
code: 'MODULE_NOT_FOUND',
requireStack: []
npm ERR! code 1
npm ERR! path D:\Documents\~College\~Coding\D&D\skill-tree
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c tsc
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\layto\AppData\Local\npm-cache\_logs\2021-06-03T13_19_01_935Z-debug.log
我注意到它说Error: Cannot find module 'D:\Documents\~College\~Coding\tsc\bin\tsc'
,我认为这是问题的根源,因为这样的文件夹~Coding\tsc
不存在,但我不明白为什么脚本试图访问该文件夹,也不我知道如何解决它吗?
这是我的 tsconfig 文件:
"compilerOptions":
"target": "ESNEXT",
"module": "ESNext",
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true
,
"include": [
"./src/**/*"
],
"exclude": [
"./node_modules",
"./.vscode",
"./templates"
]
这是我的包文件:
"name": "skill-tree",
"version": "1.0.0",
"description": "",
"scripts":
"test": "echo \"Error: no test specified\" && exit 1",
"build": "tsc"
,
"author": "Layton Burchell",
"license": "ISC",
"devDependencies":
"mongodb": "^3.6.9",
"tsc": "^2.0.3",
"typescript": "^4.3.2"
这是日志文件:
0 verbose cli [
0 verbose cli 'C:\\Program Files\\nodejs\\node.exe',
0 verbose cli 'C:\\Users\\layto\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
0 verbose cli 'run',
0 verbose cli 'build'
0 verbose cli ]
1 info using npm@7.5.2
2 info using node@v15.8.0
3 timing config:load:defaults Completed in 2ms
4 timing config:load:file:C:\Users\layto\AppData\Roaming\npm\node_modules\npm\npmrc Completed in 2ms
5 timing config:load:builtin Completed in 2ms
6 timing config:load:cli Completed in 1ms
7 timing config:load:env Completed in 1ms
8 timing config:load:file:D:\Documents\~College\~Coding\D&D\skill-tree\.npmrc Completed in 0ms
9 timing config:load:project Completed in 0ms
10 timing config:load:file:C:\Users\layto\.npmrc Completed in 0ms
11 timing config:load:user Completed in 0ms
12 timing config:load:file:C:\Program Files\nodejs\etc\npmrc Completed in 1ms
13 timing config:load:global Completed in 1ms
14 timing config:load:cafile Completed in 0ms
15 timing config:load:validate Completed in 0ms
16 timing config:load:setUserAgent Completed in 0ms
17 timing config:load:setEnvs Completed in 1ms
18 timing config:load Completed in 8ms
19 verbose npm-session cdea02db6803097a
20 timing npm:load Completed in 19ms
21 timing command:run-script Completed in 105ms
22 verbose stack Error: command failed
22 verbose stack at ChildProcess.<anonymous> (C:\Users\layto\AppData\Roaming\npm\node_modules\npm\node_modules\@npmcli\promise-spawn\index.js:64:27)
22 verbose stack at ChildProcess.emit (node:events:378:20)
22 verbose stack at maybeClose (node:internal/child_process:1067:16)
22 verbose stack at Process.ChildProcess._handle.onexit (node:internal/child_process:301:5)
23 verbose pkgid skill-tree@1.0.0
24 verbose cwd D:\Documents\~College\~Coding\D&D\skill-tree
25 verbose Windows_NT 10.0.19042
26 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\layto\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "run" "build"
27 verbose node v15.8.0
28 verbose npm v7.5.2
29 error code 1
30 error path D:\Documents\~College\~Coding\D&D\skill-tree
31 error command failed
32 error command C:\Windows\system32\cmd.exe /d /s /c tsc
33 verbose exit 1
我试过了:
使用npm i typescript -g
全局安装打字稿
使用npm i typescript --save-dev
在本地重新安装打字稿
在 Windows 控制台和 npm 脚本中运行 npx tsc
而不是仅运行 tsc
(两者都失败并出现上述相同的错误)。
【问题讨论】:
【参考方案1】:您不需要tsc
包(在您的devDependencies
中列出)。它不是来自微软。 tsc
命令行工具附带typescript
节点包。
所以卸载tsc
并重试。
【讨论】:
我安装了tsc
来尝试解决这个问题,不知道这是怎么解决的。不过谢谢,大量的帮助。
很高兴它有帮助。我猜您可能也尝试了其他方法,实际上解决了问题,但同时安装了 tsc
再次破坏了它。以上是关于用于构建打字稿的 NPM 脚本不起作用的主要内容,如果未能解决你的问题,请参考以下文章
NPM 脚本 - 配置变量和命令替换在 package.json 中不起作用