小编Jud*_*ta 的帖子

如何修复语法错误:无法在模块外部使用 import 语句?

我对这一切都很陌生,我正在尝试将 Node + Typescript 后端添加到我的 Angular 项目中。但是导入express后总是报错。

[ERROR] SyntaxError: Cannot use import statement outside a module.
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "server",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "serve": "ts-node-dev server.ts",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@types/express": "^4.17.11",
    "express": "^4.17.1",
    "ts-node": "^9.1.1",
    "ts-node-dev": "^1.1.6",
    "tslint": "^6.1.3",
    "typescript": "^4.2.4"
  },
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNEXT",                                
    "module": "commonjs", 
    "strict": false, 
    "esModuleInterop": true, 
    "skipLibCheck": true,                          
    "forceConsistentCasingInFileNames": true        
  } …
Run Code Online (Sandbox Code Playgroud)

express typescript ts-node

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

express ×1

ts-node ×1

typescript ×1