Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
131
rated 0 times [  133] [ 2]  / answers: 1 / hits: 30115  / 6 Years ago, mon, september 24, 2018, 12:00:00

I have a javascript file in the public folder and I want to import that file to components in the folder src/components.



projectFolder
publicFolder
index.html
recorder.js
srcFolder
componentsFolder
Speech.js
Speech.css


But I can't do something like this in my component:



import Recorder from '../../public/recorder'


Because I get the following error:




Module not found: You attempted to import ../../public/recorder which
falls outside of the project src/ directory. Relative imports outside
of src/ are not supported. You can either move it inside src/, or add
a symlink to it from project's node_modules/.




As I've understood it's not allowed to import outside of /src directory, so I was wondering how I could add a symlink or if you know other ways to fix it.


More From » reactjs

 Answers
21

I believe you are using create-react-app ... this is a feature included in the ModuleScopePlugin, and you can disable it by ejecting the app and editing your webpack configuration (as described in this answer).



But beware, the feature exists for a reason. The create-react-app build tool only processes the src/ directory, so for example your JavaScript outside of here will not be transpiled by Babel. Furthermore, you're typically trying to avoid polluting the global scope if you're using a bundler like Webpack. So unless you've got a really specific reason why you'd need to do this, I'd say try and move it.


[#53433] Wednesday, September 19, 2018, 6 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaceyr

Total Points: 510
Total Questions: 97
Total Answers: 116

Location: Solomon Islands
Member since Fri, Oct 8, 2021
3 Years ago
kaceyr questions
;