简体   繁体   English

为什么使用 Create React App 生成的 TypeScript 项目可以找到模块,而 Express 则不能?

[英]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.

相关问题 为什么 Express 不呈现我的 React 应用程序? - Why doesn't Express render my React app? Typescript + react-create-app 有快递问题 - Typescript + react-create-app with express problem 为什么 npx react-native init MyApp 不起作用并且不创建 React Native 项目? - Why doesn't npx react-native init MyApp not work and doesn't create a React Native project? 找不到React中所需的模块 - Can't find modules required in React 使用 create-react-app 通过 JEST 运行测试时,不会显示 TypeScript 错误 - TypeScript errors doesn't show when running tests through JEST with create-react-app Typescript找不到使用Webpack别名导入的模块 - Typescript can't find modules which are imported with webpack alias Typescript 错误并说它找不到用于导入图像等资产的模块 - Typescript errors and says it can't find modules for imports on assets such as images 无法在 create-react-app 生成的应用程序中读取环境变量 - can't read environment variables in create-react-app generated app 找不到“EJS”打字稿和快递 - Can not find 'EJS' Typescript and Express 在弹出的 react-create-app 项目测试中模拟(“单击”)不会导致重新渲染 - simulate("click") in test of ejected react-create-app project doesn't cause rerender
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM