【问题标题】:setting up typescript with webpack使用 webpack 设置打字稿
【发布时间】:2018-03-24 08:23:54
【问题描述】:

我正在使用 Typescript 开发带有 graphql 和 express 的后端 API。我正在使用 webpack 来管理项目开发和构建过程。

我正在使用 raw-loader 从文件中加载 graphql 模式和 sql 查询作为字符串。

目前我面临一些我不太了解的问题。

webpack.config.js

const path = require('path');

module.exports = {
  target: 'node',
  entry: {
    server: path.resolve(__dirname, 'server.ts'),
  },
  output: {
    filename: 'server.js',
    path: path.resolve(__dirname, 'dist'),
  },
  devtool: 'source-map',
  module: {
    rules: [
      {
        test: /\.(gql|sql)$/,
        loader: 'raw-loader',
      },
      {
        test: /\.ts$/,
        loader: 'ts-loader',
      },
    ],
  },
  resolve: {
    extensions: ['.ts', '.js'],
  },
  watch: {
    ignored: /node_modues/,
  },
};

server.ts

import express from 'express';
import morgan from 'morgan';
import bodyParser from 'body-parser';
import graphqlHTTP from 'express-graphql';

import { affairSchema } from './src/schemas';
import affairRoot from './src/resolvers/affairs';

const app = express();

app.use(morgan('combined'));
app.use(bodyParser.json());

app.use(
  '/graphql',
  graphqlHTTP({
    schema: affairSchema,
    rootValue: affairRoot,
    graphiql: true,
  })
);

const port = process.env.PORT || 4000;
app.listen(port, () => console.log('Server waiting on ', port));

错误:

ERROR in ./node_modules/graphql/index.mjs
88:0-148:42 Can't reexport the named export 'typeFromAST' from non EcmaScript module (only default export is available)
 @ ./node_modules/graphql/index.mjs
 @ ./src/schemas/index.ts
 @ ./server.ts

ERROR in ./node_modules/graphql/index.mjs
88:0-148:42 Can't reexport the named export 'valueFromAST' from non EcmaScript module (only default export is available)
 @ ./node_modules/graphql/index.mjs
 @ ./src/schemas/index.ts
 @ ./server.ts

ERROR in ./node_modules/graphql/index.mjs
88:0-148:42 Can't reexport the named export 'valueFromASTUntyped' from non EcmaScript module (only default export is available)
 @ ./node_modules/graphql/index.mjs
 @ ./src/schemas/index.ts
 @ ./server.ts

ERROR in C:\Users\houssem\Projects\ctc\services\affairs\src\schemas\index.ts
./src/schemas/index.ts
[tsl] ERROR in C:\Users\houssem\Projects\ctc\services\affairs\src\schemas\index.ts(5,28)
      TS2307: Cannot find module './affairs.gql'.

ERROR in C:\Users\houssem\Projects\ctc\services\affairs\services\database\engineer.ts
./services/database/engineer.ts
[tsl] ERROR in C:\Users\houssem\Projects\ctc\services\affairs\services\database\engineer.ts(7,19)
      TS2307: Cannot find module './engineer.sql'.
.
.
.
.
.

【问题讨论】:

  • 也许通过webpack-node-externals使用外部?
  • @H.B.成功了,非常感谢。
  • 好的,我想这可以算作一个答案。

标签: typescript express webpack ts-loader


【解决方案1】:

您可以使用webpack-node-externals 来防止捆绑节点模块。这样可以摆脱这样的错误,当然在运行应用的时候也需要node_modules

【讨论】:

  • 所以我还需要部署 node_modules 文件才能让我的服务器运行。
  • 应该是这样,是的。
猜你喜欢
  • 2016-02-06
  • 2018-10-09
  • 2022-06-28
  • 1970-01-01
  • 2017-10-24
  • 1970-01-01
  • 2017-03-19
  • 2017-12-25
  • 2016-04-23
相关资源
最近更新 更多