[英]Why a TypeScript project generated with Create React App can find modules and an Express one doesn't?
I use WebStorm, I created a React project and an Express project with a build-in wizard.我使用 WebStorm,我创建了一个 React 项目和一个带有内置向导的 Express 项目。 I manually added TypeScript to the latter.
我手动将 TypeScript 添加到后者。 Here's my
tsconfig.json
for React:这是我的
tsconfig.json
用于 React:
{
"compilerOptions": {
"target": "es2017",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
}
And here's one for express:这是一个快递:
{
"compilerOptions": {
"sourceMap": true,
"outDir": "output",
"esModuleInterop": true,
"noImplicitAny": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"strict": true,
"isolatedModules": true,
"module": "esnext",
"target": "es2017",
"moduleResolution": "node",
"allowSyntheticDefaultImports": true,
"resolveJsonModule": true,
"allowJS" : true
},
"exclude": [
"node_modules"
],
"include": [
"source/**/*"
]
}
For the express project I needed to set "type": "module"
in package.json
.对于快递项目,我需要在
package.json
中设置"type": "module"
。
The problem: when I use import
in the React project I don't need to add the extension .js
to file names, but in the Express project it's necessary, otherwise I'm getting ERR_MODULE_NOT_FOUND
.问题:当我在 React 项目中使用
import
时,我不需要将扩展名.js
添加到文件名中,但在 Express 项目中这是必需的,否则我会得到ERR_MODULE_NOT_FOUND
。
import {serviceRouter} from "./ServiceRouter";
vs对比
import {serviceRouter} from "./ServiceRouter.js";
When using ES6 modules ( "type": "module"
), extensions are mandatory, see https://nodejs.org/api/esm.html#esm_mandatory_file_extensions使用 ES6 模块时(
"type": "module"
),扩展是强制性的,见https://nodejs.org/api/esm.html#esm_mandatory_file_extensions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.