electron-webpack icon indicating copy to clipboard operation
electron-webpack copied to clipboard

typescript cannot find webpack alias module

Open dreamerblue opened this issue 4 years ago • 4 comments

  • Version: 2.7.4

I have cloned a new quick-start project and followed this link to install TS add-ons. https://webpack.electron.build/add-ons#typescript

Then in src/main/index.ts, I imported some module in src/main (alias as @) and src/common (alias as common).

Now eletcron-webpack dev runs with no error. But my IDE (such as VS Code) shows errors in editor:

Cannot find module '@/someModule'. ts(2307)
Cannot find module 'common/someModule'. ts(2307)

dreamerblue avatar Oct 06 '19 16:10 dreamerblue

Just ran into this issue myself. Anyone able to find any follow-up info?

mikerogne avatar Nov 30 '19 20:11 mikerogne

@livemeta Thanks for your exploration, but it only works for Sass, not TypeScript.

Forgot to reply. Actually I found the solution a few times after posting the issue. Just add these below (the path to @ and common should be replaced by yours) to tsconfig.json, so that TypeScript can resolve these paths.

Tested on TypeScript 3.6.3 and 3.7.2.

"paths": {
  "@/*": ["src/main/*"],
  "common/*": ["src/common/*"]
}

dreamerblue avatar Dec 16 '19 13:12 dreamerblue

Also encountered this problem too, the solution from @dreamerblue solution is quite effective.

The key thing here is keep the "*" at the end of all keys and values

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@/*": ["app/*"]
    }
}

Caldis avatar Jan 28 '20 13:01 Caldis

I have the same problem. I have tried the solutions posted here without success.

The application seems to run correctly. However, I noticed that also electron-webpack (2.8.2 and ewts 4.0.1) is complaining about not finding the module only the firs time it starts. Any additional reload doesn't make the message to reappear:

package.json: `

"electronWebpack": { "commonSourceDirectory": "src/common", "staticSourceDirectory": "src/static", "title": true, "main": { "sourceDirectory": "src/main", "webpackConfig": "src/main/webpack.additional.js"
}, "renderer": { "sourceDirectory": "src/renderer", "webpackConfig": "src/renderer/webpack.additional.js" } }, `

webpack.additional.js: module.exports = { resolve: { alias: { "@com": path.resolve(__dirname, '../common') } } }; tsconfig.json: `

"extends": "./node_modules/electron-webpack/tsconfig-base.json", "baseUrl": ".", "paths": { "@com/": ["src/common/"] }, `

Webstorm complains about this (SystemInformation is in the common directory): `

import { SystemInformation } from '@com/system_information'; `

electron-webpack complains about it too: `

┏ Main -----------------------

ERROR in D:/Projects/sd/teapot/src/renderer/app/app.component.ts(7,35): TS2307: Cannot find module '@com/system_information' or its corresponding type declarations.

┗ ---------------------------- ┏ Renderer -------------------

ERROR in D:/Projects/sd/teapot/src/renderer/app/app.component.ts(7,35): TS2307: Cannot find module '@com/system_information' or its corresponding type declarations.

┗ ----------------------------

`

biziosan avatar Nov 09 '20 15:11 biziosan