[英]How to create/config a NodeTS(TypeScript NodeJS) project?
我正在尝试创建并运行一个简单的 TypeScript NodeJS 项目。 以下是我的代码:
服务器.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
入口文件设置为index.js
,因此nodemon
正在尝试同时运行server.js
和index.js
,但它们不存在。 解决此问题的最简单方法 - 例如,假设您已经使用tsc
编译了 TS 文件 - 是将package.json
中的入口点main
更改为build/server.js
。
所以你必须做的:
typescript
以便能够转译您的代码:npm install typescript
npm start
, you have to make sure your code is transpiled, that is, "compiled" to vanilla JavaScript, so add this script to your package.json
:{
"scripts": {
// ...
"build": "tsc"
}
}
package.json
中的main
更新为build/server.js
。npm run build
然后最后npm start
。完毕。 您的服务器将启动
我在答案更新之前的提示:
此外,为nodemon
( nodemon.json
) 创建一个配置文件将使您的事情变得更简单,请查看文档。
我修改了我的tsconfig.json
文件,如下所示:
{
"name": "back",
"version": "1.0.0",
"description": "",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "nodemon src/server.ts"
},
"author": "",
"license": "ISC",
"dependencies": {
"@types/express": "^4.17.13",
"express": "^4.18.1",
"mongoose": "^6.5.0"
},
"devDependencies": {
"nodemon": "^2.0.19",
"ts-node-dev": "^2.0.0",
"typescript": "^4.7.4"
}
}
创建的更改在这些行中:
"main": "index.js"
到"main": "server.js"
"start": "nodemon build/server.js"
到"start": "nodemon src/server.ts"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.