如何让我的node.js服务器在生产模式下运行?

r7s23pms  于 2023-11-17  发布在  Node.js
关注(0)|答案(1)|浏览(123)

我有一个用typescript编写的node服务器,使用调试器和vs代码运行良好。

....
import { logIn, getLogins, findUser } from "./src/logins";
const app = express ();
app.use(express.json());
....
....

字符串
但是当我尝试运行node dist\app.js时,

SyntaxError: Cannot use import statement outside a module


因此,according to this post,我将"type": "module",添加到package.json。但现在我得到错误

[ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\quilk\source\repos\ridehub-server\dist\src\logins' imported from C:\Users\quilk\source\repos\ridehub-server\dist\app.js


请帮帮忙!package.json:

{
  "name": "ridehub-server",
  "version": "1.0.0",
  "description": "learning",
  "main": "dist\\app.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node app.ts",
    "dev": "tsx ..."
  },
  "repository": {
    "type": "git",
    "url": "quilkin\\ridehub-server"
  },
  "author": "quilkin",
  "license": "ISC",
  "dependencies": {
    "body-parser": "^1.20.2",
    "cors": "^2.8.5",
    "dotenv": "^16.3.1",
    "express": "^4.18.2",
    "linq": "^4.0.2",
    "method-override": "^3.0.0",
    "mysql": "^2.18.1",
    "nodemailer": "^6.9.7",
    "xml2js": "^0.6.2"
  },
  "devDependencies": {
    "@types/cors": "^2.8.15",
    "@types/express": "^4.17.20",
    "@types/node": "^20.8.8",
    "tsx": "^3.14.0",
    "typescript": "^5.2.2"
  }
}


tsconfig.json:

{

  "compilerOptions": {
    "composite": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": ["node"],
    "paths": {
      "@/*": ["./src/*"]
    },
    "outDir": "dist",
    "allowSyntheticDefaultImports": true,
    "lib": ["ES2020"],
    "module": "ES2020",
    "moduleResolution": "node",
    "target": "ES2020",                               
    "esModuleInterop": true,                             
    "forceConsistentCasingInFileNames": true,            
    "strict": true,                                      
    "skipLibCheck": true                                 
  },
  "references": [
    { 
        "path": "../ridehub-common",
    }
]
}

hs1rzwqc

hs1rzwqc1#

我找到了答案here.
问题在于为导入命名文件扩展名:

import { logIn} from "./src/logins";

字符串
添加.ts扩展名(逻辑扩展名,因为这是我导入的文件的名称)给出错误

An import path can only end with a '.ts' extension when 'allowImportingTsExtensions' is enabled.ts(5097)


实际上需要的是添加一个.js扩展。

import { logIn} from "./src/logins.js";


这对我来说似乎完全不合逻辑-在该文件夹中没有这样的文件,它只在运行'tsc'后在dist文件夹中创建。
哦,你不能使用@/logins.js,你必须使用./src/logins.js或其他什么。

相关问题