简体   繁体   English

找不到模块“./App.svelte”或其对应的类型声明

[英]Cannot find module './App.svelte' or its corresponding type declarations

I have a setup that integrates electron with svelte along with typescript support.我有一个集成了 electron 和 Svelte 以及 typescript 支持的设置。

when I run the rollup script to compile svelte app, i am getting cannot find module ./App.svelte error as shown below.当我运行rollup脚本来编译 svelte 应用程序时,我得到了找不到模块./App.svelte错误,如下所示。

Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './App.svelte' or its corresponding type declarations.

Here's my package.json configuration:这是我的package.json配置:

{
  "name": "tapwire-electron-first",
  "productName": "tapwire-electron-first",
  "version": "1.0.0",
  "description": "My Electron application description",
  "main": "dist/index.js",
  "scripts": {
    "electron-start": "tsc && electron-forge start",
    "electron-package": "electron-forge package",
    "electron-make": "electron-forge make",
    "electron-publish": "electron-forge publish",
    "electron-lint": "eslint --ext .ts .",
    "svelte-build": "rollup -c",
    "svelte-dev": "rollup -c -w",
    "svelte-start": "sirv public",
    "svelte-validate": "svelte-check",
    "start": "run-p svelte-dev electron-start"
  },
  "keywords": [],
  "author": {
    "name": "nateshmbhat",
  },
  "license": "MIT",
  "config": {
    "forge": {
      "packagerConfig": {},
      "makers": [
        {
          "name": "@electron-forge/maker-squirrel",
          "config": {
            "name": "tapwire_electron_first"
          }
        },
        {
          "name": "@electron-forge/maker-zip",
          "platforms": [
            "darwin"
          ]
        },
        {
          "name": "@electron-forge/maker-deb",
          "config": {}
        },
        {
          "name": "@electron-forge/maker-rpm",
          "config": {}
        }
      ]
    }
  },
  "devDependencies": {
    "@electron-forge/cli": "^6.0.0-beta.53",
    "@electron-forge/maker-deb": "^6.0.0-beta.53",
    "@electron-forge/maker-rpm": "^6.0.0-beta.53",
    "@electron-forge/maker-squirrel": "^6.0.0-beta.53",
    "@electron-forge/maker-zip": "^6.0.0-beta.53",
    "@rollup/plugin-commonjs": "^15.1.0",
    "@rollup/plugin-node-resolve": "^9.0.0",
    "@rollup/plugin-typescript": "^6.0.0",
    "@types/node": "^14.11.2",
    "@typescript-eslint/eslint-plugin": "^2.34.0",
    "@typescript-eslint/parser": "^2.34.0",
    "cross-env": "^7.0.2",
    "electron": "10.1.3",
    "eslint": "^7.10.0",
    "eslint-plugin-import": "^2.22.1",
    "npm-run-all": "^4.1.5",
    "rollup": "^2.28.2",
    "rollup-plugin-livereload": "^2.0.0",
    "rollup-plugin-svelte": "^6.0.1",
    "rollup-plugin-terser": "^7.0.2",
    "svelte": "^3.29.0",
    "svelte-check": "^1.0.55",
    "svelte-preprocess": "^4.3.2",
    "typescript": "^4.0.3"
  },
  "dependencies": {
    "concurrently": "^5.3.0",
    "electron-reload": "^1.5.0",
    "electron-squirrel-startup": "^1.0.0",
    "sirv-cli": "^1.0.6"
  }
}

Turns out i had to install @tsconfig/svelte as a dev dependency first then extend this with my tsconfig.json as shown below :原来我必须先安装@tsconfig/svelte作为开发依赖项,然后用我的tsconfig.json扩展它,如下所示:

Install @tsconfig/svelte安装@tsconfig/svelte

npm i --save-dev @tsconfig/svelte

Add this tsconfig as the base config file in your tsconfig.json :将此 tsconfig 添加为tsconfig.json中的基本配置文件:

{
  "extends": "@tsconfig/svelte/tsconfig.json",
}

The culprit on my end was a stale global.d.ts .我的罪魁祸首是陈旧的global.d.ts I had /// <reference types="@sveltejs/kit" /> set, but was trying to change the project to vanilla Svelte.我设置了/// <reference types="@sveltejs/kit" /> ,但试图将项目更改为 vanilla Svelte。 Fixing it to have /// <reference types="svelte" /> instead did the trick.将其修复为具有/// <reference types="svelte" />反而起到了作用。

I had the same problem with a default react-ts vite project when compiling Typescript with tsc使用tsc编译 Typescript 时,我在默认的react-ts vite 项目中遇到了同样的问题

I had to remove following line in tsconfig.json我不得不删除tsconfig.json中的以下行

"skipLibCheck": false,

If Natesh's answer doesn't work for you, this file should work as a basic shim:如果Natesh 的回答对您不起作用,则此文件应作为基本 shim:

// svelte-shim.d.ts

declare module "*.svelte" {
    import type { ComponentType } from "svelte";
    const component: ComponentType;
    export default component;
}

This essentially tells TypeScript that any .svelte files it doesn't otherwise know what to do with should be read in as Svelte components.这实际上告诉 TypeScript 任何它不知道如何处理的 .svelte 文件都应该作为 Svelte 组件读入。 You won't get any of their exported props or methods, but if all you need is to use their basic ComponentType interface, this should work fine.你不会得到他们导出的任何道具或方法,但如果你只需要使用他们的基本ComponentType接口,这应该可以正常工作。

I've got a similar problem.我有一个类似的问题。 I built a component library with Svelte and trying to build it with rollup.我用 Svelte 构建了一个组件库,并尝试用 rollup 构建它。 In my index.tsx file I export my components like:在我的 index.tsx 文件中,我导出我的组件,例如:

export { default as MyComponent } from "./components/MyComponent.svelte";

When I try to run build, I get the following error: Plugin typescript: @rollup/plugin-typescript TS2307: Cannot find module './components/MyComponent.svelte' or its corresponding type declarations.当我尝试运行构建时,我收到以下错误:插件 typescript:@rollup/plugin-typescript TS2307: 找不到模块 './components/MyComponent.svelte' 或其相应的类型声明。

Here is my tsconfig:这是我的 tsconfig:

  "extends": "@tsconfig/svelte/tsconfig.json",
  "include": ["src"],
  "exclude": ["node_modules/*", "__sapper__/*", "public/*"],
  "compilerOptions": {
    "target": "es2016",
    "module": "esnext",
    "outDir": "dist",
    "strict": true,
    "moduleResolution": "node",
    "esModuleInterop": true,
    "declaration": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "importsNotUsedAsValues": "remove"
}
```

and my rollup.config:
```
import svelte from 'rollup-plugin-svelte';
import resolve from 'rollup-plugin-node-resolve';
import postcss from 'rollup-plugin-postcss';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import json from '@rollup/plugin-json';
import autoPreprocess from 'svelte-preprocess';
import typescript from '@rollup/plugin-typescript';
import nodeResolve from '@rollup/plugin-node-resolve';

const packageJson = require("./package.json");

export default {
  input: "src/index.tsx",
  output: [
    {
      file: packageJson.main,
      format: "cjs",
      sourcemap: false,
    },
    {
      file: packageJson.module,
      format: "esm",
      sourcemap: false,
    },
  ],
  plugins: [
    json({ compact: true }),
    svelte({
      preprocess: autoPreprocess(),
    }),
    resolve(),
    peerDepsExternal(),
    nodeResolve(),
    typescript({ sourceMap: true, rootDir: "./src" }),
    postcss({
      extensions: [".css"],
    }),
  ],
};

Has anyone faced this issue too?有没有人也遇到过这个问题? Thanks!谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 找不到模块“graphql-hooks”或其相应的类型声明 - Cannot find module 'graphql-hooks' or its corresponding type declarations 如何解决找不到找不到模块'../home/featuredRooms'或其对应的类型声明。? - how to solve cannot find Cannot find module '../home/featuredRooms' or its corresponding type declarations.? 为什么我收到此错误找不到模块“nestjs/common”或其相应的类型声明 - Why I got this error Cannot find module 'nestjs/common' or its corresponding type declarations VueJS/Typescript - 找不到模块“./components/Navigation”或其相应的类型声明 - VueJS/Typescript - Cannot find module './components/Navigation' or its corresponding type declarations ReScript,TypeScript:找不到模块“@rescript/react/src/ReactDOM.gen”或其相应的类型声明 - ReScript, TypeScript: Cannot find module '@rescript/react/src/ReactDOM.gen' or its corresponding type declarations React,Storybook - TS2307:找不到模块“按钮”或其相应的类型声明 CAN SB RESOLVE? - React,Storybook - TS2307: Cannot find module 'Button' or its corresponding type declarations CAN SB RESOLVE? 找不到模块“@react-navigation/native”或其相应的类型声明 - Cannot find module '@react-navigation/native' or its corresponding type declarations Docker/Next js:: 找不到模块“@mui/x-date-pickers/AdapterDateFns”或其相应的类型声明 - Docker/Next js :: Cannot find module '@mui/x-date-pickers/AdapterDateFns' or its corresponding type declarations 在 vercel 上部署我的 next.js 项目,找不到模块“react-icons/Fa”或其相应的类型声明 - deploy my next.js project on vercel, Cannot find module 'react-icons/Fa' or its corresponding type declarations 在 Angular 项目中尝试导入 SDK 时找不到模块或其对应的类型声明 - Can't find module or its corresponding type declarations when trying to import SDK in Angular Project
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM