Ts-loader: consider webpack alias prop for typescript npm modules

Created on 8 Jul 2016  路  8Comments  路  Source: TypeStrong/ts-loader

webpack allow to define alias for node_modules.
We can write

  alias: {
      'any-ts-npm-package': path.resolve(__dirname, 'node_modules/any-ts-npm-package')
  } 

For example, packages p1,p2 uses different versions of 'any-ts-npm-package' with different api;

in this case compiler don't know about webpack alias and do typechecking for p1 (dependent 'any-ts-npm-package' of version 0.1) and for p2 (dependent on 'any-ts-npm-package' of version 0.1).

wontfix

Most helpful comment

It appears as though this is supported as there's tests to validate this functionality.

I came here by the way of modernizr-loader and I cannot get the modernizr module to be recognized by the TypeScript compiler.

import Modernizr from 'modernizr';
error TS2307: Cannot find module 'modernizr'.

All 8 comments

Does "moduleResolution": "node" intsconfig.json resolve this issue?

I've run into the same problem using webpack's resolve.alias configuration option and ES6 module imports. However, using the commonjs require('module') method seems to work. Here's a snippet of what my code looks like:

example.ts

import config from 'config';

webpack.config.js

    resolve: {
        alias: {
            config: path.resolve(__dirname, 'config')
        }
    }

I'm using [email protected], [email protected], and [email protected].

It appears as though this is supported as there's tests to validate this functionality.

I came here by the way of modernizr-loader and I cannot get the modernizr module to be recognized by the TypeScript compiler.

import Modernizr from 'modernizr';
error TS2307: Cannot find module 'modernizr'.

@stepancar @samuelmaddock @michaelwoods

It's can be solved by using paths in tsconfig

webpack.config.js

resolve: {
    alias: {
        'any-ts-npm-package': path.resolve(__dirname, 'node_modules/any-ts-npm-package'),
        config: path.resolve(__dirname, 'config')
    }
}

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "any-ts-npm-package": ["node_modules/any-ts-npm-package"],
      "config": ["config"]
    }
  }
}

read more ts/handbook/module-resolution

@finico any idea how this might work if you wanted to then generate declaration files for this project and publish them? i can't seem to get the paths to be honored in a package that consumes the declaration files

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

Closing as stale. Please reopen if you'd like to work on this further.

Was this page helpful?
0 / 5 - 0 ratings