I'm writing a typescript module that uses a typescript based project ( winston
).
This project has bundled its types in a index.d.ts
file and has no @types/winston
package that can be used as a "devDependency".
As I understand it, the typescript
dependency should reside in the devDependencies
section as it's not a runtime needed dependency. The winston
dependency should of course reside in the dependencies
section as it will be used in runtime.
Thing is, if I run npm install --only=dev
, the winston
package will not be downloaded, and my tsc
execution will fail since I don't have the winston
types at hand during compilation ( error TS2307: Cannot find module 'winston'
).
Even if I somehow workaround this error, it's clear that I won't be able to use the package's typings during typescript compilation as they were not downloaded.
So what should be the approach here? Do every typescript bundled package that provides its own types should reside in the devDependencies
section of the package.json
? But then what about npm install --production
that will miss the dependency and will fail in runtime?
I tried adding winston
to both devDependencies
and the dependencies
section of the package.json
, but running npm install --only=dev
will not download packages that reside in the dependencies
section as well (not sure if 'by design' or bug in npm logic).
package.json
{
"name": "tst",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"devDependencies": {
"@types/node": "^12.7.4",
"typescript": "^3.6.2"
},
"dependencies": {
"winston": "^3.2.1"
}
}
test.ts
import * as winston from "winston";
const logger = winston.someMethod();
The command everyone normally uses is simply npm install
. The additional parameter —only=dev
that you're using is telling NPM to only download the packages inside the devDependencies section.
It's not a switch to be used for dev vs production builds! This is why you're seeing errors.
As you're creating a TypeScript library, there's no need for definitions in @types as you've found out. We can instead include our definitions in the package itself:
Ensure that TypeScript is also creating definitions files in your output - check your output folder (usually the dist or lib folder) if you see files with the extension .d.ts alongside the .js files then great, otherwise look up how to set up tsconfig.json to emit definitions until it does.
Once you've got the types, in package.json there is the property “main” where you declare the entry point to your compiled .js package in the output folder. There's also a property “types” which you'll need to add and point at the .d.ts file with the same name as your main entry point.
That should be all you need to do to get things up and running if you haven't already.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.