typescript - 导入其他 typescript 模块

标签 typescript node-modules

我正在尝试从 node_modules 中的 typescript 文件导入。该文件未转译并使用 typescript 语法:

user.ts:

export const getUserFromToken = (token: string) => {
  return axios.get('/user', {
    headers: { authorization: `Bearer ${token}` }
  }).then(({ data }) => data)
}

我是这样导入的:import { getUserFromToken } from 'common-files/api/user'common-files 是在 package.xml 中注册的模块。 JSON.

尝试编译时,我得到:

../commons/api/user.ts
Module parse failed: C:\Users\xxxx\Desktop\xxxx\commons\api\user.ts 
Unexpected token (9:38)
You may need an appropriate loader to handle this file type.
| export const getUserFromToken = (token: string) => {
|   return axios.get('/user', {
|     headers: { authorization: `Bearer ${token}` }

我相信,这导致它没有编译 ts 文件,因为当我删除字符串类型时,它变成了一个有效的 es6 文件,并且它可以正确启动。

在我的 tsconfig.json 文件中我有:

"exclude": [
  "node_modules",
  "build",
  "scripts",
  "acceptance-tests",
  "webpack",
  "jest",
  "src/setupTests.ts"
]

我在想,也许要排除 node_modules,除了包 common-files,但不知道该怎么做。有什么好方法可以实现我想要的吗?

最佳答案

@ShaunLuttin 回答了一般情况下的问题。我的问题是因为我将 create-react-app 与 typescript 脚本一起使用,并且它们的 webpack 配置不允许使用来自模块的原始 typescript 文件。我决定转译我所有的文件,并生成一个可以用作模块的构建。它有效,但是我确保在处理前端项目时不包含任何仅在节点环境中有效的内容。

关于typescript - 导入其他 typescript 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45470552/

相关文章:

node.js - http.Server 的 'connect' 事件从未被调用

javascript - "export ' __platform_browser_private__ ' was not found in ' @angular/platform-b​​rowser'

node.js - 如何从另一个 EC2 实例中的 Node js 调用 KONG API(正在运行 EC2 实例)?

typescript - 无法将文件转换为 Blob

angular - 禁用 Kendo UI 日历小部件中的 future 日期

javascript - 在 javascript 函数中读取 Typescript 类成员

node.js - 更新我的node_modules中某个包的每个存在

node.js - 手动注册 Node 模块

javascript - Angular 2 绑定(bind)到计算 getter 会出现调试错误

javascript - youtube-dl 显示错误 : Command failed with exit code 1