parcel: Cannot find module error when importing an static file using typescript
🐛 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 |
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 1
- Comments: 20 (3 by maintainers)
Create a
.d.tsfile in your project and put :It will instruct TypeScript to use
anyfor all imports ending with.png. It it still doesn’t work try to restart VSCode.you need to add the d.ts to “include” of tsconfig.json
It works for me, working with expo-reactnative, I just made in the root a file called
index.d.tsIt worked here. I’m using create-react-app and put it into react-app-env.d.ts file.
@fathyb not sure to understand how it works. I’ve created the
.d.tsin 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 foundCreating
index.d.tsinside the project’s root folder is what did the trick for me. Thanks!if you entry point fileName - index.ts you need to name file at will but not index.ts
Hey Michael, I should have updated this thread sooner! I managed to find a solution for Parcel V2 (which is what I am using).
I got the solution from the Parcel V2 TypeScript documentation, where it shows you the
tsconfig.jsonconfig.In the end, what worked for me was putting this in my tsconfig:
The only thing I changed from the documentation was setting
"preact"to"react-jsx". Of course, change it to suit your environment.I am not at work at the moment, so this is from the top of my head. Let me know if you still get problems, I may have missed something. 😃
Just create the file src > react-app-env.d.ts with the following contetn?
/// <reference types="react-scripts" />
Solved.