Reputation: 553
I'm wondering the best way for me to use a full path for my project.
For example, if I am importing a component, I don't want to do import Component from '../components/Component'
, rather just do import Component from 'app/components/Component';
Can someone help me out?
Upvotes: 16
Views: 14484
Reputation: 3020
You can use absolute path by figuring out the name of your app in the package.json name section and then using it in front of your absolute path i.e
import Component from MyApp/components/Component
Here's more information on describing the issue https://github.com/facebook/react-native/issues/3099
Upvotes: 9
Reputation: 39
I tried many approaches. The one package.json
file per folder sounds a bit too much for me. Also, I had issues with that, as it would start complaining about my import React from 'react'
statements.
The best solution I found was this one, based on babel.config.js
(if you're using Expo). It's using babel-plugin-module-resolver, a 40KB lib, millions of downloads. With that you'll have to:
babel.config.js
to something like:module.exports = function(api) {
api.cache(true);
return {
presets: ['module:metro-react-native-babel-preset'],
plugins: [
[
'module-resolver',
{
root: ['.'],
extensions: [
'.ios.ts',
'.android.ts',
'.ts',
'.ios.tsx',
'.android.tsx',
'.tsx',
'.jsx',
'.js',
'.json',
],
alias: {
app: './src/app',
helpers: './src/helpers',
...
},
},
],
],
};
};
This should be enough to work. But, if you're like me, using typescript
, you may also want to please your IDE by preventing it from complaining about the imports.
tsconfig.json
as follows:{
"compilerOptions": {
...
"baseUrl": ".",
"paths": {
"app/*": ["./src/app/*"],
"helpers/*": ["./src/helpers/*"],
...
},
...
}
}
I struggled a bit to understand the configuration, because that article is showing app
as the base folder, whereas I'm using src
. No big deal, just had to translate it to my scenario. My file structure is as follows, btw:
project
- android
- ios
- src
- app
- helpers
index.js
Upvotes: 3
Reputation: 2911
On top of everyone's answer
You can actually create a package.json
anywhere in other folders
For instance you have :
▾ assets/
▸ home/
▸ png/
favicon.png
icon.png
splash.png
▾ components/
▸ display/
▸ filters/
▸ forms/
▾ images/home/
feedback.js
index.js
▸ crud/
If you create package.json
inside assets folder with the value
{"name": "assets"}
Then you can import in any other JS using assets/png/someimage.png
Also you can use babel module resolver, with the babel.config.js
that is included (if you're using expo)
Modify it to this:
module.exports = function (api) {
api.cache(true);
return {
presets: ["babel-preset-expo"],
plugins: [
[
"module-resolver",
{
root: ["./"],
alias: {
assets: "./assets",
components: "./components",
crud: "./crud",
helpers: "./helpers",
},
},
],
],
};
};
Upvotes: 1
Reputation: 389
// Use this:
import MyUtilFn from 'utils/MyUtilFn';
// Instead of that:
import MyUtilFn from '../../../../utils/MyUtilFn';
You can use babel-resolver: npmjs.com/package/babel-plugin-module-resolver. May it can help you
Upvotes: 2
Reputation: 11830
To have an absolute path,
I am considering your package.json is in the root.
Inside, your package.json add a new property name with value whatever you want i.e
"name": "root"
and then import all your routes relative to that route
import Component from root/components/Component
Upvotes: 10