-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
create-react-app + jsconfig.json paths: modules/scripts not found #7205
Comments
Hey @ppalmeida, sorry you're facing this issue. Could you try updating your Storybook version to the latest stable versions - if that doesn't help, please let me know. Also, keep in mind that Create React App doesn't support paths officially, and we've not tested that in Storybook. Can you also share a test repository that I could have a look at? A basic reproduction only. |
PS: Sorry for the slow reply, I've been away. |
Hey, @mrmckeb. Actually I have to say I was completely WRONG when said that CRA was assuming So, I think we can close this issue. For those who is interested in having CRA working with Step 1I prefer not to eject CRA, so I went with some Step 2After install it, I created a const path = require("path");
const resolve = dir => path.resolve(__dirname, dir);
module.exports = function(config, env) {
config.resolve.alias = Object.assign(config.resolve.alias, {
"@mycompany/app": resolve("src/packages/app"),
"@mycompany/core": resolve("src/packages/core"),
"@mycompany/http": resolve("src/packages/http"),
// etc...
});
return config;
}; Step 3Then, I created the {
"compilerOptions": {
"baseUrl": "./src",
"paths": {
"@mycompany/app/*": ["packages/app/*"],
"@mycompany/core/*": ["packages/core/*"],
"@mycompany/http/*": ["packages/http/*"],
// etc... you got the idea...
}
},
"exclude": ["node_modules", "build", "coverage", "dist", "lib"]
} And then you would ask: "But why use this file if you already did the rewired webpack in step 2?". Because Step 4: JESTJEST: now, you must run your tests, right? So, add these lines to your // etc...
"jest": {
"moduleNameMapper": {
"^@mycompany/app(/?)(.*?)$": "packages/app$1$2",
"^@mycompany/core(/?)(.*?)$": "packages/core$1$2",
"^@mycompany/http(/?)(.*?)$": "packages/http$1$2",
}
}, Pretty nasty, ahn? The keys are RegExp that Jest uses to find your files. And "Why did you use 2 groups in your regex, if they are at the end of the string anyway?". Well, this is how Webpack (or Jest, I'm not sure) will match your files: So, these both will work: import MyAwesomeModule from '@mycompany/app'
import MyOtherAwesomeModule from '@mycompany/app/folder1/folder2/MyOtherAwesomeModule'; Step 5: StorybookWell, let's put Storybook in full-control mode, so we can apply the same concepts to Storybook as well. To do that, you must create a const path = require("path");
const resolve = dir => path.resolve(__dirname, dir);
module.exports = async ({ config }) => {
config.resolve = Object.assign(config.resolve, {
alias: {
"@mycompany/app": resolve("../src/packages/app"),
"@mycompany/core": resolve("../src/packages/core"),
"@mycompany/http": resolve("../src/packages/http"),
}
});
return config;
}; Note that, since this file is inside Bottom Line:I don't know if there is a better strategy here. Maybe there is a really better way. Keep As I put in the link in the beginning, I think CRA will soon have it's "way" of offering path out of the box. But, for now, I don't think it's worth to do all these configurations. For me, it's too much effort. Anyway, maybe it can help someone. cheers |
@ppalmeida - what about ESLINT? |
Hey, @yairEO. About the
But I'm not sure. didn't test it. I'll take a look. Talking about ESLint, what's the problem you're facing? Because the ESLint works well here without any other additional configuration. Do you want to share some particular problem you're facing at? |
@xppalmeida - I was facing problems where eslint complained about not being able to resolve my absolute paths but I've managed to fix them! Using this resolver: eslint-import-resolver-node eslint configuration file (relevant part)settings: {
'import/resolver': {
'node': {
'paths': ['./', './src']
}
} .env
jsconfig.json{
"compilerOptions": {
"jsx": "react",
"baseUrl": "./",
"paths": {
"stories/*": ["stories/*"],
"shared/*": ["src/shared/*"],
},
},
"include": ["src/**/*", "stories/**/*"]
} only with Storybook seems to work well with the above configuration and nothing else. |
Really good to know! Thank you! |
Describe the bug
When using CRA and "jsconfig.json": if I set "path" property to solve imports alias, the app works as expected running CRA, but Storybook crashes saying it can't resolve imports.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
Shouldn't Storybook take the paths from jsconfig.json file - or solve it someway with full control mode, where I could solve the imports using "resolve" with Webpack?
Code snippets
This is my current
jsconfig.json
file (Using it with Create React App it works perfectly and the app builds with no errors):This is the
.storybook/config.js
file.Also, I've tried to use the full control mode, with this config:
System:
The text was updated successfully, but these errors were encountered: