I have a package written in typescript. Main module is located in src
and is called Popover.ts
. Main index file is called index.js
and main
field in package.json
is pointed to that file. It has such content:
exports.Popover = require('./src/js/Popover');
Then in another package I try to include this plugin. I installed it with npm. And here issues come. In javascript everything works well:
const Popover = require('popover');
But when I try to import it in typescript file (demo.ts) it does not work:
import Popover from 'popover';
First, PhpStorm highlights popover
with a red line and says Can not find module 'popover'
. Second, when I build demo with webpack, I got no errors, however build file does not contain the contents of Popover.ts
.
I do not understand why it happens and how I can fix it.
Update: I managed to get rid of red highlighting in PhpStorm by setting "moduleResolution": "node"
in tsconfig.json
(thanks to @user254694). However then I got a different problem: build failed. Webpack generated such an error:
Error: TypeScript emitted no output for
F:\dev\js\plugins\popover\demo\npm\node_modules\popover\src\js\Popover.ts.
By default, ts-loader will not compile .ts files in node_modules.
I googled this error and come to the following solution. Add allowTsInNodeModules
option to ts-loader
:
loader: 'ts-loader',
options: {
allowTsInNodeModules: true
}
Then I added such lines to tsconfig.js
:
"include": [
"node_modules/popover/src/**/*",
"node_modules/popover/index.js",
"node_modules/popover/typings/**/*"
]
While this eliminated the webpack error and it compiled successfully, I got red highlight again.
PS: I generated declaration types in typings
directory and now my package.json
looks like this:
"main": "index.js",
"types": "typings/index.d.ts",
Still wonder how to make it working.