【问题标题】:Importing react-relay-network-modern in cypress fails在柏树中导入 react-relay-network-modern 失败
【发布时间】:2023-03-04 12:00:01
【问题描述】:

问题

大家好,

我正在使用 TypeScript 3.0.3cypress 并尝试在我的组件中加载 react-relay-network-modern 模块。但是,它总是告诉我该模块不存在,尽管它在我的 node_modules 文件夹中并在我的 package.json 中定义。我对relay-runtime 没有任何错误,而且我的中继工作正常。

当我像这样进行常规导入时:

import * as RelayNetworkModern from 'react-relay-network-modern';

我收到以下错误,即找不到模块。

/src/relayEnvironment.ts
[tsl] ERROR in /src/relayEnvironment.ts(8,37)
      TS2307: Cannot find module 'react-relay-network-modern'.

即使我在 tsconfig.json 和 webpack 预处理器中设置别名或导入子文件夹 react-relay-network-modern/(es|lib|...) 时,我也会遇到相同的错误。


配置

我认为最好添加我的配置文件:

/cypress/plugins/webpack.config.js

const webpack = require( '@cypress/webpack-preprocessor' );
const path = require('path');

const options = {
  watchOptions: {},
  webpackOptions: {
    resolve: {
      extensions: [ '.web.js', '.js', '.json', '.web.jsx', '.jsx', '.tsx', '.ts', '.mjs' ],
      modules: [
        path.resolve( __dirname, '../../node_modules' ),
        path.resolve( __dirname, '../../src' ),
      ],
      alias: {
        'react-relay-network-modern': path.resolve( __dirname, '../../node_modules/react-relay-network-modern/es' ),
        src: path.resolve( __dirname, '../../src' ),
      }
    },
    module: {
      rules: [
        {
          type: 'javascript/auto',
          test: /\.mjs$/,
          include: /node_modules/,
          use: [],
        },
        {
          test: /\.ts(x)?$/,
          exclude: [ /node_modules/ ],
          use: [
            {
              loader: 'babel-loader',
              options: {
                presets: [ '@babel/preset-react' ],
                plugins: [ 'relay' ],
              },
            },
            {
              loader: 'ts-loader',
            },
          ],
        },
      ],
    },
  },
};

module.exports = webpack( options );

/tsconfig.json

{
  "compilerOptions": {
    "baseUrl": ".",
    "target": "es6",
    "jsx": "react",
    "skipLibCheck": true,
    "noImplicitAny": false,
    "strict": true,
    "lib": ["dom", "es6", "es2016", "es2017.object"],
    "types": [ "cypress", "react" ],
    "typeRoots": [ "node_modules/@types", "types" ],
    "paths" : {
      "react": ["node_modules/@types/react"],
      "react-relay-network-modern": ["node_modules/react-relay-network-modern/es"],
      "src/*": ["src/*"],
      "components/*": ["src/*"]
    }
  },
  "include": [
    "node_modules/cypress",
    "src/*/*.ts"
  ]
}

【问题讨论】:

    标签: typescript relay cypress relaymodern


    【解决方案1】:

    好的,经过大量修补和测试多个 tsconfig 选项后,我想通了。

    原来我只需要在我的 tsconfig 中设置以下 2 个选项:

        "allowJs": true,
        "moduleResolution": "node"
    

    所以我的新 tsconfig.json 看起来像这样:

    {
      "compilerOptions": {
        "baseUrl": ".",
        "target": "es6",
        "jsx": "React",
        "skipLibCheck": true,
        "noImplicitAny": false,
        "strict": true,
        "allowJs": true,
        "moduleResolution": "node",
        "lib": ["dom", "es6", "es2016", "es2017.object"],
        "types": [ "cypress", "react" ],
        "typeRoots": [ "node_modules/@types", "types" ],
        "paths" : {
          "react": ["node_modules/@types/react"],
          "src/*": ["src/*"],
          "components/*": ["src/*"]
        }
      },
      "include": [
        "node_modules/cypress",
        "src/*/*.ts"
      ]
    }
    

    【讨论】:

      猜你喜欢
      • 2017-10-18
      • 2017-09-30
      • 2017-10-21
      • 2017-10-15
      • 2020-05-21
      • 2019-09-23
      • 2018-05-17
      • 2017-11-23
      • 2017-11-27
      相关资源
      最近更新 更多