【发布时间】:2021-01-14 13:11:46
【问题描述】:
我正在运行我的服务:
"start": "NODE_ENV=production node --optimize_for_size --trace-warnings --experimental-json-modules --es-module-specifier-resolution=node --no-warnings dist/server-new/server.js"
package.json - 注意它是模块类型
{
"name": "some-name",
"license": "UNLICENSED",
"version": "0.0.0",
"description": "",
"type": "module",
"engines": {
"node": "14.x",
"npm": "6.14.x"
},
...
}
tsconfig.json
{
"extends": "../../tsconfig",
"compilerOptions": {
"noEmit": false,
"rootDir": "." /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */,
"outDir": "../../dist" /* Redirect output structure to the directory. */
},
"module": "ESNext",
"include": ["./*.ts", "package.json"],
"resolveJsonModule": true
}
server.ts - 将 api.js 作为 ES6 模块导入
import cluster from 'cluster';
import os from 'os';
import App from './api.js';
...
App.listen(port, () => {
console.log(`express is listening on port ${port}`);
});
api.ts - 能够导入 express 但在导入 GraphQL 通用 JS 模块时遇到问题
import compression from 'compression';
import express from 'express';
import { graphqlHTTP } from 'express-graphql';
import { buildSchema } from 'graphql';
import CompanyController from './Controllers/CompanyController';
const App = express()
.use(compression())
....
);
export default App;
启动脚本出错:
SyntaxError: The requested module 'graphql' is expected to be of type CommonJS, which does not support named exports. CommonJS modules can be imported by importing the default export.
参考import { buildSchema } from 'graphql';
所以我尝试了这个:
import * as graphQL from 'graphql';
const { buildSchema } = graphQL;
但是我在这里收到了 TS 错误...也许我现在需要解决这个问题,但不确定如何解决:
TypeError: buildSchema is not a function
也试过了:
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { buildSchema } = require('graphql');
错误:ReferenceError: require is not defined
所以我不确定如何将这两个项目导入 api.ts。
【问题讨论】:
标签: javascript node.js graphql