我正在尝试从 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/