问题描述:

Due to legal barriers we have our node_modules in another folder(lets call it "external"), so following sentence from TS docs does not apply: "Packages in node_modules/@types of any enclosing folder are considered visible; specifically, that means packages within ./node_modules/@types/, ../node_modules/@types/, ../../node_modules/@types/, and so on".

I believe that is so, because my folder is in ./external/node_modules and not ./node_modules.

So can this somehow work? I tried that way as well:

"compilerOptions": {

"module": "amd",

"target": "es5",

"sourceMap": true,

"removeComments": true,

"allowJs": false,

"pretty": true,

"experimentalDecorators": true,

"typeRoots": ["./external/node_modules/@types"]

}

Folder structure:

- external

-- node_modules

--- @types

- tsconfig.json

Now when I try to import angular I receive "Cannot find module angular" error message.

网友答案:

It sounds like the compiler can't actually find the module.
The compiler is looking for both the module and the definition file for it, so it's not enough to specify the typeRoots, you probably also need to set the baseUrl in the compiler options:

Base directory to resolve non-relative module names

As it's written in the Module Resolution baseUrl section:

Using a baseUrl is a common practice in applications using AMD module loaders where modules are “deployed” to a single folder at run-time. The sources of these modules can live in different directories, but a build script will put them all together.

Setting baseUrl informs the compiler where to find modules. All module imports with non-relative names are assumed to be relative to the baseUrl.


Edit

I've never been in your scenario, so this more of a guess...
But looking at the steps described in the docs about module resolution:

a non-relative import will follow the Node.js resolution logic, first looking up a file, then looking up an applicable folder. So import { b } from "moduleB" in source file /src/moduleA.ts would result in the following lookups:

  1. /root/src/node_modules/moduleB.ts
  2. /root/src/node_modules/moduleB.tsx
  3. /root/src/node_modules/moduleB.d.ts
  4. /root/src/node_modules/moduleB/package.json (if it specifies a "typings" property)
  5. /root/src/node_modules/moduleB/index.ts
  6. /root/src/node_modules/moduleB/index.tsx
  7. /root/src/node_modules/moduleB/index.d.ts

  8. /root/node_modules/moduleB.ts

  9. /root/node_modules/moduleB.tsx
  10. /root/node_modules/moduleB.d.ts
  11. /root/node_modules/moduleB/package.json (if it specifies a "typings" property)
  12. /root/node_modules/moduleB/index.ts
  13. /root/node_modules/moduleB/index.tsx
  14. /root/node_modules/moduleB/index.d.ts

  15. /node_modules/moduleB.ts

  16. /node_modules/moduleB.tsx
  17. /node_modules/moduleB.d.ts
  18. /node_modules/moduleB/package.json (if it specifies a "typings" property)
  19. /node_modules/moduleB/index.ts
  20. /node_modules/moduleB/index.tsx
  21. /node_modules/moduleB/index.d.ts

It seems that it shouldn't be looking in ./external/...

相关阅读:
Top