Meteor ES6 modules relative paths - javascript

Just came back to meteor, and trying to figure out how to structure a project, I'm getting stuck with how to handle relative paths, my project structure is like this:
-root
--client
----components
-----layout
...... MainLayout.jsx
--server
--lib
.. routes.jsx
If I try to import in my routes.jsx file like this './../client/components/layout/MainLayout.jsx' I get the error 'Cannot find module './../client/components/layout/MainLayout.jsx...
However if I move MainLayout.jsx to the root folder, i can reach the component file like this without problems './../MainLayout.jsx'.
Maybe I'm missing something here? as I said just returned to JS programming.

When the router.js file in the root. You have use instead of
'./../client/components/layout/MainLayout.jsx'
this:
'../client/components/layout/MainLayout.jsx'
or just:
'../client/components/layout/MainLayout'

Related

Do I have to place any scripts/modules I create and want to import within the node_modules folder?

I am creating a VSCode extension, and following the getting started guide (https://code.visualstudio.com/api/get-started/your-first-extension) have used yeoman scaffold to get started. I created a new file, newModule.js in the same directory and want to import it as a module for use in the main extension.js script. I then do:
const newModule = require('./newModule.js');
This throws an error:
cannot find module 'newModule' require stack: -
This problem disappears if I copy my file to the node_modules folder created by default. I would like to know what is going on here, and what the best way of handling imports is when working with javascript/Node.js/vs-extensions.
I also notice that node_modules folder is not pushed to github by default, why?
The node_modules folder is for storing all the code from the libraries and packages you are using. It is excluded from git because it is a waste of space and a distraction to store them all in your versioning-control, as you can just re-download them anytime.
Just put your module in the same /src directory, and use the import syntax to import it, instead of require.
import newModule from './newModule';
For example, see how it is done in this sample code.
Please instead
const newModule = require('./newModule.js');
Try this
import newModule from './newModule');
// ^^ Do not use file extension
Also make sure that the file you are calling is in the same directory

How can I solve an error where a default export is not found at the file level I specified?

I'm trying to build a simple Covid-19 tracker using an API and React tocreate interactive displays. However, when I try to run the project, I keep getting this error:
Here is my App.js folder, where it appears I am importing them correctly:
App.js file
Lastly, here is my index.js file in my components folder, which is where I am exporting the files:index.js
I am not sure what the problem is. Initially, I thought it may be a problem with how it's navigating to the files, but when I change that, nothing appears to be different, and the error persists. Would anyone be able to help?
Here is what my file structure looks like for reference: file structure
You have typo in word "defualt"
you should try:
import {default as Cards} from "./Cards/Cards";
You misspelled 'default'

Error: Error parsing triggers while deploying cloud functions - Firebase

I'm using renderToString function from react-dom (on my server side). The code looks like (+/-):
import Home from './app/containers/Home';
const app = express();
app.get('**', (req, res) => {
const html = renderToString(<Home />);
res.set('Cache-Control', 'public, max-age=600, s-maxage=1200');
res.send(html);
});
Everything goes smooth, until I try to deploy it on the server.
Example error in the console:
Error: Error parsing triggers: Cannot find module 'store/Home/actions'
When it appears?
If I change my path to some other component, which does not use any other components (only modules from node, like react or react-redux) it works correctly.
But if I try to use some component which uses other components and imports them, e.g.:
var _CreateUser = require('components/Pages/CreateUser'); (it's in the rendered component)
Now it will fail with error:
Error: Error parsing triggers: Cannot find module 'components/Pages/CreateUser'
So currently Im stuck, because I have to use my whole app on server side, not just a single component which doesn't import anything :)
Why does it work this way? Why does it fail? Is it bad webpack config fail?
Looking forward for any help. Thank you.
Note, as I said above, if I render to string some component with any imports (that doesn't use any other component in it) - the server side rendering works fine and Im able to see the renderedToString content before page loads.
Everywhere you import local modules you need to include the directory in the path otherwise it will search node_modules for a named package and ultimately fail.
require('./store/Home/actions');
Or:
import HomeActions from './store/Home/actions';
...depending on which import style you're using. An accurate directory is always needed as a part of the import/require statement.
You're using a relative path, cd into and deploy from the functions directory so it's correct.
It looks like your Home component is inside functions/app/containers/Home and you need access to the file functions/app/store/Home/actions.
From your containers/Home file, you need to go up two directories to the app folder, then go down two directories to correct file. So
import HomeActions from '../../store/Home/actions'`.
Each ../ represents going up one directory to the parent folder. We went from
functions/app/containers/Home to
functions/app/containers/ to
functions/app and then we can specify which path to continue on from there with
store/Home/actions

I'd like to reuse code from another module in my Nodejs backend

I'm using the Yeoman Generator Angular Fullstack and I'd like to reuse JS code from different directories within my server directory. I'm referencing the file that has the functions I want like:
var something = require('/.path');
I get the error: "Cannot find module" in my terminal.
I tried a number of variations of the path working through all of the levels from server level down to file level where the given functions are contained. I looked at a few tutorials:
http://www.sitepoint.com/understanding-module-exports-exports-node-js/
AND
https://www.launchacademy.com/codecabulary/learn-javascript/node/modules
I clearly missed something. Each module of my nodejs has a controller with an exports.create function. All of my code for each module is contained within my exports.create function accept for other required modules. I have no problem requiring underscore or other libraries in my Node/Bower modules by the way.
To be as detailed as can be, I expected
var something = require('./directory/directory.controller.js');
var something = require('/.path');
The path you are using is likely incorrect. Probably you want to open the file called path.js contained in the same folder of the file from which you are importing it. In order to do that, you should change the import as follows :
var something = require('./path');
./path is a relative path where . stands for current directory.
/.path is an absolute path. In this case require is importing a hidden file in the root directory. I guess is not what you want.

Change configuration folder structure in sails.js

I just want to have another folder structure in sails.js. For example, something like:
/config
/application
routes.js
policies.js
/db
connections.js
...
It seems like when lifting application sails look for each of that files (console.log to /config/application/policies.js works when application getting lifted) anyway it does not work.
I try to add paths.config value to .sailsrc but it changes nothing. I an using sails version 0.10.5 now. Is there a way to do it?
I have found the solution. Actually, everything is very simple.
sails requires files from config folder recursively and tries to recognize them as configuration files. My problem was that I had copied most files as is:
// config/customfolder/connections.js
module.exports.connections = {
...
}
and here is the problem to require it. To make it work I just need to export config object as default export.
// config/customfolder/connections.js
module.exports = {
...
}

Categories

Resources