【发布时间】:2023-03-04 12:00:01
【问题描述】:
问题
大家好,
我正在使用 TypeScript 3.0.3 和 cypress 并尝试在我的组件中加载 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