如何创建并配置NodeTS(TypeScript NodeJS)项目?启动报错求助
TypeScript Node.js 项目启动报错:找不到 index.js 模块
我正在搭建并运行一个简单的TypeScript Node.js项目,相关代码如下:
server.ts
import express from 'express'; const app = express(); const PORT = 3000; app.listen(PORT , ()=>{ console.log('Server is listening on port $PORT'); });
tsconfig.json
{ "compilerOptions": { "target": "esnext", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ "module": "commonjs", /* 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. */ "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. */ "skipLibCheck": true /* Skip type checking all .d.ts files. */ } }
package.json
{ "name": "back", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "start": "nodemon build/server.js" }, "author": "", "license": "ISC", "dependencies": { "@types/express": "^4.17.13", "express": "^4.18.1", "mongoose": "^6.5.0", "nodemon": "^2.0.19" } }
执行npm start时,出现以下错误:
> back@1.0.0 start > nodemon build/server.js [nodemon] 2.0.19 [nodemon] to restart at any time, enter `rs` [nodemon] watching path(s): *.* [nodemon] watching extensions: js,mjs,json [nodemon] starting `node build/server.js index.js` node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module 'C:\Users\A\\Test\back\index.js' at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12) at node:internal/main/run_main_module:17:47 { code: 'MODULE_NOT_FOUND', requireStack: [] } [nodemon] app crashed - waiting for file changes before starting...
问题原因及解决方法
package.json main字段冲突
package.json中main设置为index.js,但实际编译后的入口是build/server.js。nodemon会自动把main字段的值作为额外参数传给node,导致执行命令变成node build/server.js index.js,node尝试加载不存在的index.js从而报错。- 解决:将
package.json里的main字段改为build/server.js,或者直接删除该字段(因为scripts中已经明确指定了启动入口)。
- 解决:将
缺少TypeScript编译步骤
报错信息显示nodemon尝试启动build/server.js,但如果还没执行TypeScript编译,该文件可能不存在或内容异常。- 解决:
- 先安装TypeScript依赖:
npm install typescript --save-dev - 在
package.json的scripts中添加编译命令,并修改start命令确保先编译再启动:"scripts": { "test": "echo \"Error: no test specified\" && exit 1", "build": "tsc", "start": "npm run build && nodemon build/server.js" } - 或者使用
ts-node直接运行TypeScript文件,省去手动编译步骤:安装ts-node和@types/node,将start命令改为nodemon --exec ts-node src/server.ts
- 先安装TypeScript依赖:
- 解决:
server.ts模板字符串错误
当前console.log使用单引号包裹字符串,$PORT无法被解析为变量值,需要改成反引号实现模板字符串解析:console.log(`Server is listening on port ${PORT}`);
内容的提问来源于stack exchange,提问作者lovecode
相关产品推荐
相关产品推荐

