伙计们,
我将 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"
]
}
答案 0 :(得分:0)
好吧,经过大量的修改和测试了多个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"
]
}