在柏树中导入 react-relay-network-modern 失败

Importing react-relay-network-modern in cypress fails

问题

大家好,

我正在使用 TypeScript 3.0.3cypress 并尝试在我的组件中加载 react-relay-network-modern 模块。但是,它总是告诉我该模块不存在,尽管它在我的 node_modules 文件夹中并在我的 package.json[= 中定义46=]。 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"
  ]
}

好的,经过大量修改和测试多个 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"
  ]
}