4

Maybe it's a duplicate but I've searched for an hour and haven't found the answer.

I have a node module named a-module which contains some .ts files (for example a.ts)

I have another node module b-module which has a-module among its dependencies.

I want to import some .ts file from a-module to b-module. In some file within b-module I write:

import a = require('a-module/a');
console.log(a);

When then I'm trying to compile b-module with tsc, is says

Cannot find external module 'a-module/a'.

What am I doing wrong?

P.S. I have ArcticTypescript plugin for SublimeText, and seems that it is enough intelligent to find a-module/a. Why then tsc doesn't manage to locate my file?

P.P.S My file structure looks like that

b-module/
  node_modules/
    a-module/
      a.ts
  b.ts

I'm trying to import a.ts to b.ts.

3
  • Can you post your directory structure? Like basarat said the TypeScript compiler needs to know where the module is located. Commented May 29, 2015 at 9:20
  • @vanhelgen I've updated my question with file structure provided Commented May 29, 2015 at 9:29
  • I know this is old question, but I haven't seen "require" and "import" used together in one line. Can someone point me to some docs somewhere where this is mentioned as correct syntax? Because AFAIK you should use "require" or "import", not both together. And import have syntax without "=". It's really strange to me. 🤔 Commented Aug 14, 2022 at 5:47

1 Answer 1

0

import a = require('a-module/a');

You need to either use relative paths i.e. ../a-module/a or declare it for TypeScript explicitly i.e. declare module "a-module/a".

Sign up to request clarification or add additional context in comments.

4 Comments

Where should I declare my a-module/a if it is a file a.ts inside my some another repository installed via npm install?
Inside your module in some .d.ts file
If I write import a = require('./node_modules/a-module/a') then it compiles. But it looks weird. Why not to import like CommonJS does allow.
I guess this is due to the fact that TypeScript has to know the type of the module upfront so it insists on knowing the exact one to be loaded. Node is resolving the dependencies at runtime so it hasn't this restriction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.