首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >webpack从node_modules用户导入ts文件

webpack从node_modules用户导入ts文件
EN

Stack Overflow用户
提问于 2021-09-21 11:53:21
回答 1查看 34关注 0票数 2

我的nextJS应用程序一切正常,但是当我尝试从node_modules/workspace/dtos.ts中导出.ts文件时,给出了一条错误消息

.ts、.tsx文件也可以很好地工作

DTOs文件

代码语言:javascript
复制
export * from '.workspace/dtos';

错误

代码语言:javascript
复制
error - ./node_modules/.workspace/dtos.ts
Module parse failed: Unexpected token (11:7)
You may need an appropriate loader to handle this file type


> export interface APIDefinition {
|     cors?: CORSConfig;

tsconfig.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "allowJs": true,
    "allowSyntheticDefaultImports": true,
    "baseUrl": ".",
    "experimentalDecorators": true,
    "forceConsistentCasingInFileNames": true,
    "jsx": "preserve",
    "jsxFactory": "jsx",
    "lib": [
      "es2020",
      "dom"
    ],
    "esModuleInterop": true,
    "isolatedModules": true,
    "module": "commonjs",
    "moduleResolution": "node",
    "noEmit": true,
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noImplicitThis": false,
    "resolveJsonModule": true,
    "skipLibCheck": true,
    "strict": false,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "target": "esnext"
  },
  "exclude": [
    "node_modules",
    "build",
    "**/*.json",
    "**/*.js"
  ],
  "include": [
    "node_modules/.workspace/dtos.ts",
    "next-env.d.ts",
    "**/*.ts",
    "**/*.tsx"
  ]
}

.babelrc

代码语言:javascript
复制
{
  "presets": [
    [
      "next/babel",
      {
        "preset-react": {
          "importSource": "theme-ui",
          "runtime": "automatic",
          "throwIfNamespace": false
        }
      }
    ]
  ]
}

next.config.js

代码语言:javascript
复制
const { i18n } = require('./next-i18next.config');

module.exports = {
  i18n
};

也许在配置中添加一些异常是值得的?

EN

回答 1

Stack Overflow用户

发布于 2021-09-21 14:46:16

Webpack错误是说它不知道如何处理TS-语法。

我建议在webpack的配置中添加ts-loader,但不排除node_modules

代码语言:javascript
复制
rules: [
  {
    test: /\.tsx?$/,
    use: "ts-loader",
  },

不要忘记安装typescript和ts-loader。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69268443

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档