运行mock报错
来源:7-10 使用 Apollo-server 快速创建 mock 数据

兰博万
2023-04-11
npm run mock
water-drop-mobile@0.0.0 mock D:\work\learn\drop\water-drop-mobile
node ./mock/index.js
file:///D:/work/learn/drop/water-drop-mobile/node_modules/@graphql-tools/utils/esm/helpers.js:1
import { parse } from ‘graphql’;
^^^^^
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.
For example:
import pkg from ‘graphql’;
const { parse } = pkg;
at ModuleJob._instantiate (internal/modules/esm/module_job.js:98:21)
at async ModuleJob.run (internal/modules/esm/module_job.js:137:5)
at async Loader.import (internal/modules/esm/loader.js:165:24)
at async Object.loadESM (internal/process/esm_loader.js:68:5)
1回答
-
黑石
2023-04-11
这个问题是跟模块引入方式有关,你可以按照老师的 tsconfig.json 里的配置进行修改,看看我们代码之间的差异。
{ "compilerOptions": { "target": "ESNext", "useDefineForClassFields": true, "lib": ["DOM", "DOM.Iterable", "ESNext"], "allowJs": false, "skipLibCheck": true, "esModuleInterop": false, "allowSyntheticDefaultImports": true, "strict": true, "forceConsistentCasingInFileNames": true, "module": "ESNext", "moduleResolution": "Node", "resolveJsonModule": true, "isolatedModules": true, "noEmit": true, "jsx": "react-jsx", "paths": { "@/*": [ "./src/*" ] } }, "include": ["./"], "references": [{ "path": "./tsconfig.node.json" }] }
主要看看
esModuleInterop module allowSyntheticDefaultImports
这三个属性的设置。
022023-04-12