Import Node.js npm modules (node_modules) and declarations in TypeScript

I think it would be great to import modules directly from the node_modules directory without having to manually provide the declaration file for it (suppose the declaration is provided by the module itself). I suppose the problem is with the dependencies that appear with the declarations (file paths can be resolved relative to the module, but this can lead to duplicates and the compiler unable to handle this). Currently, working with node modules is very inconvenient, because simply installing from the npm repository is not enough, and we must manually search for declarations and provide them in our project. Let's say that our project depends on 10 node modules (all of them have declarations), and in a year we would like to update them. We would have to manually search for new ads and say that we have about 20 such projects (this will be a nightmare).Maybe it should be possible to directly import the .ts file from the node module?

Do you have any suggestions?

This is a repeated post from CodePlex to hear your opinions ...

+5
source share
2 answers

If you use grunt-typescript, then I have a pull request that solves this, at least for me. See https://github.com/k-maru/grunt-typescript/pull/36

From README Retrieval Request

Working with modules in node_modules (i.e. npm)

The standard way to use npm packages is to provide a definition file that points the package to typescript and imports the module from there.

///<reference path="path/to/mod.d.ts" />
import mod = module('mod')

The typescript compiler converts importto nodejs require.

var mod = require('mod')

, npm npm , .

node_modules grunt import npm , npm, .

import npm typescript

import npmModule = module('node_modules/npmModule/foo')

- . typescript typescript node_modules/npmModule/foo.d.ts, - javascript require npmModule/foo, .

+2

, node typescript. - 0.x -.

typescript. Github .d.ts, :

https://github.com/borisyankov/DefinitelyTyped https://github.com/soywiz/typescript-node-definitions

: https://github.com/Diullei/tsd. , , .

, , .d.ts. , , , , - , .

+1

All Articles