0

I don't know what I am doing wrong with this path in data.js. It worked in another project, but doesn't in this one with image:

require('../../assets/images/icon.png');

This is the folder structure in VS:

→ expo
→.vscode
↓ app
  → tabs
    ⚛️_layout.jsx
    ⚛️+not-found.jsx
    data.js
↓ assets
  → fonts
  ↓ images
    🖼️ icon.png
4
  • 1
    have you noticed the ":" instead of the semicolon at the end of your import? Just a typo here or is it the same in the code? Commented Aug 8 at 10:35
  • Oops.... sorry, yes, that's a typo accident. Do you know what could be the path to images? 🙏🏼 Commented Aug 8 at 14:43
  • I assume that the data.js is inside the app and tabs folder, right? Also it's "icon.png", you got a dash in there Commented Aug 9 at 15:23
  • I'm sorry about the dash, that's a typo too... yes, the data folder is inside the (tabs) folder ..... its ↓ app →(tabs) →data and the rest Commented Aug 10 at 13:06

1 Answer 1

0

With the corrections of the file path (dot and semicolon), require type imports are the old style which isnt typically used in react. If applied correctly you would have to use:

var img = require('../../assets/images/icon.png');

For this to work, you need to have the property type in your package.json defined with commonjs :

"type": "commonjs"

the prefered approach for react and generally javascript is the module import, for that you need to change the type property to the following:

"type": "module"

Then you can import like you'd usually do within react:

import img from '../../assets/images/icon.png';

Your path should otherwise be correct. You can check this by holding CMD on Mac or CTRL on Windows/Linux and the clicking the part of your path you want to jump to. That's how you can check if it lands where it should without code execution

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.