无法导入打字稿模块

时间:2017-10-31 15:20:02

标签: typescript npm webpack

我使用以下样板作为开头:

https://github.com/rokoroku/react-redux-typescript-boilerplate

我正在尝试导入只有ts文件的node_modules。我从本地目录安装它,如下所示:

/abc-def
  /src
    /index.ts
  /package.json

这个dep的package.json看起来像这样:

{
  "name": "@abc/def",
  "version": "1.0.0",
  "license": "UNLICENSED",
  "main": "./src/index.ts",
}

我安装了它,工作正常

yarn add file:../abc-def

它显示在@abc/def下的node_modules和样板文件的package.json中:

"dependencies": {
  "@abc/def": "file:../abc-def"
}

虽然,当我运行项目时,typescript抱怨由于某种原因无法找到依赖项,我无法导入它。

import Def from '@abc/def';
  

[at-loader]中的错误./src/containers/App/index.tsx:10:23       TS2307:找不到模块'@ abc / def'。

我尝试用ts文件替换js文件,但它可以正常工作,但它不想直接导入ts文件。必须有一些ts配置才能更改,但我不确定哪些配置。

我浏览了TS module resolution documentation,但仍无法找到修改tsconfig.json文件的正确方法。

知道为什么我不能直接导入ts文件吗?

由于

修改

刚刚发现如果我将条目文件移动到我的模块的根文件夹,因此将src/index.ts移动到index.ts,它运行正常。打字稿分辨率似乎没有从main文件中读取package.json字段。这是一个错误吗?

我输出了解析堆栈,有趣的是它忽略了main中的package.json字段:

======== Resolving module '@abc/def' from '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/src/containers/App/index.tsx'. ========
Explicitly specified module resolution kind: 'NodeJs'.
Loading module '@abc/def' from 'node_modules' folder, target file type 'TypeScript'.
Directory '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/src/containers/App/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'
Directory '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/src/containers/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'
Directory '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/src/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'
Found 'package.json' at '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def/package.json'.
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def.ts' does not exist.
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def.tsx' does not exist.
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def.d.ts' does not exist.
'package.json' does not have a 'typings' field.
'package.json' does not have a 'types' field.
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def/index.ts' does not exist.
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def/index.tsx' does not exist.
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@abc/def/index.d.ts' does not exist.
Scoped package detected, looking in 'abc__def'
File '/Users/alexmngn/Workspace/react-redux-typescript-boilerplate/node_modules/@types/abc__def.d.ts' does not exist.
Directory '/Users/alexmngn/Workspace/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'
Directory '/Users/alexmngn/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'
Directory '/Users/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'
Directory '/node_modules' does not exist, skipping all lookups in it.
Scoped package detected, looking in 'abc__def'

0 个答案:

没有答案