Parcel: Cannot find module error when importing an static file using typescript

Created on 27 May 2018  路  11Comments  路  Source: parcel-bundler/parcel

馃悰 bug report

If you try to import an image, in normal javascript, to get the url of the file it just works:

import characterImage from '@client/assets/img/character.png'

But when I use typescript I get this error on the vscode console

Cannot find module '@client/assets/img/character.png'.

馃帥 Configuration (.babelrc, package.json, cli command)

this is my tsconfig

{
    "compilerOptions": {
        "strict": false,
        "module": "commonjs",
        "moduleResolution": "node",
        "types": ["node", "jest"],
        "newLine": "LF",
        "outDir": "lib",
        "target": "es2017",
        "sourceMap": true,
        "declaration": true,
        "jsx": "preserve",
        "lib": [
            "es2017",
            "dom"
        ],
        "noUnusedLocals": true,
        "noUnusedParameters": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "experimentalDecorators": true,
        "baseUrl": "./src",
        "paths": {
            "@client/*": ["./client/*"],
            "@server/*": ["./server/*"]
        }
    },
    "include": [
        "**/*.ts"
    ],
    "exclude": [
        ".git",
        "node_modules"
    ]
}

馃 Expected Behavior

It should just load the image.

馃槸 Current Behavior

I get the error I just said above.

馃拋 Possible Solution

No sure, is maybe because an image doesn't have types?

馃敠 Context

So the app just works and ignores the fact that there is an error with typescript. Is very annoying for development to have false errors all over the place.

馃實 Your Environment

| Software | Version(s) |
| ---------------- | ---------- |
| Parcel | 1.8.1
| Node | v10.2.1
| npm/Yarn | yarn 1.7.0| Operating System | macos 10.13.4

Bug

Most helpful comment

Create a .d.ts file in your project and put :

declare module '*.png'

It will instruct TypeScript to use any for all imports ending with .png. It it still doesn't work try to restart VSCode.

All 11 comments

Are you using built-in TS support or the TS plugin?

@fathyb any idea?

Im not using the ts plugin, also parcel is working and not complaining. Is with the development tools

So parcel is able to resolve this asset, but vscode complains?

But you don't get the image url either, if you import it through parcel?

I get the image but I'm not sure if is hiding the typescript error.

Typescript compiles even with errors.

Create a .d.ts file in your project and put :

declare module '*.png'

It will instruct TypeScript to use any for all imports ending with .png. It it still doesn't work try to restart VSCode.

@fathyb thank you, that worked

@fathyb not sure to understand how it works. I've created the .d.ts in root level of my project and declaring a module for svg the way you did it but I still got lint errors.

So I've declared it in the file where I try the import and I got:
Invalid module name in augmentation, module '*.svg' cannot be found

I also tried creating a .d.ts file in the root with that content, but it didn't work. :(

you need to add the d.ts to "include" of tsconfig.json

if you entry point fileName - index.ts you need to name file at will but not index.ts

Create a .d.ts file in your project and put :

declare module '*.png'

It will instruct TypeScript to use any for all imports ending with .png. It it still doesn't work try to restart VSCode.

It worked here. I'm using create-react-app and put it into react-app-env.d.ts file.

Was this page helpful?
0 / 5 - 0 ratings