javascript 请求的模块'pay20.pay20.pay20.pay20.pay20.pay20.pay20.pay20.pay20.pay20.pay20.pay20'应为类型

bweufnob  于 2023-01-24  发布在  Java
关注(0)|答案(4)|浏览(150)

我的服务使用:
"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"
    },
...
}
    • 系统配置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;
    • 在 * start * 脚本**期间出错: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中。
GraphQL docs

kknvjkwl

kknvjkwl1#

真讨厌

import pkg1 from 'express-graphql';

import pkg from 'graphql';

const { graphqlHTTP } = pkg1;

const { buildSchema } = pkg;
jecbmhm3

jecbmhm32#

按照以下步骤操作,如果仍然不起作用,请更新节点版本。

import graphql from 'graphql';

const { buildSchema } = graphql;
w51jfk4q

w51jfk4q3#

在Windows和节点14上运行面向ES2022的ESM模块时出现此问题。
更新到节点16修复了该问题。

dbf7pr2w

dbf7pr2w4#

当您的本地机器节点版本比您项目中使用的节点版本(检查packahe.json文件)太旧时,会发生这种情况。使用NVM(节点版本管理器)来使用与项目相同的节点版本或更高的稳定节点版本。

相关问题