dom*_*ack 12 npm express typescript package.json
我有以下内容index.ts:
import { User } from "./datatypes"
import express from 'express';
console.log("hello world")
Run Code Online (Sandbox Code Playgroud)
这是我的package.json:
{
"name": "simple_app",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "tsc index.ts && node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"dotenv": "^16.0.0",
"express": "^4.17.3"
},
"devDependencies": {
"@types/express": "^4.17.13",
"@types/node": "^17.0.23",
"ts-node": "^10.7.0",
"typescript": "^4.6.3"
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的 tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"resolveJsonModule": true,
"target": "es6",
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist"
},
"lib": ["es2015"]
}
Run Code Online (Sandbox Code Playgroud)
当我写 时npm run start,我得到:
Module '"/mnt/c/Users/raffa/Desktop/simple_app/node_modules/@types/express/index"' can only be default-imported using the 'esModuleInterop' flag
2 import express from 'express';
~~~~~~~
node_modules/@types/express/index.d.ts:133:1
133 export = e;
~~~~~~~~~~~
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.
Run Code Online (Sandbox Code Playgroud)
我怎么解决这个问题?
qrs*_*gky 26
如果您tsc使用特定文件路径从命令行调用,它将忽略您的tsconfig.json
打字稿文档(https://www.typescriptlang.org/docs/handbook/tsconfig-json.html):
在命令行上指定输入文件时,tsconfig.json 文件将被忽略。
这意味着esModuleInterop如果您调用 tsconfig.json 的部分将不起作用tsc index.ts。
可以使用 来解决这个问题import * as express from 'express';,但是忽略 TS 配置可能会引起其他问题。
tsc -w && node index.js(来自评论之一)的一个问题是-w, 会使其等待而不是自行终止,这意味着您将无法到达“&&”之后的部分。
所以,你不会到达这个node index.js部分。
(但至少这一次,tsc没有得到一个特定的文件,因此配置不会被忽略)
另外,在您的配置中, 被outDir设置为dist,因此编译后的文件将转到那里。因此,您应该使用以下内容:
tsc && node ./dist/index.js
| 归档时间: |
|
| 查看次数: |
27923 次 |
| 最近记录: |