Why nodejs runs other js file first before index.js file? - node.js

I have created a simple nodejs project to see how nodejs runtime execution flow works. In my project folder I have initiated a nodejs project with npm init -y command. Then added "type": "module" to the package.json file. After that I have created two files
index.js with following content
console.log("index.js")
import x from './test.js'
test.js with following content
console.log("test.js")
export default {}
When I tried to execute the index.js file with node index.js I got the result as follows
test.js
index.js
But I expected the otherway around. Could someone explain why this is the behavior I am getting. Also please advice me, is there any way to enforce nodejs to go through the index.js file first before the other one?

Related

Error: Cannot find module 'C:\Users\Elie\Projects\MEAN\task-manager\api\index.js'

I have been following along this youtube playlist: https://www.youtube.com/watch?v=P3R-8jj3S7U. We got to the part where we finished a model and are ready to test. Run nodemoncommand to start the API and I get this error
Here is the file in question as well
Thanks for the help
UPDATE: In the package.json file i changed main to the exact file path of index.js you can see the package.json file here. This seemed to produce a better result but still didnt fix the issue. The error now reads clean - exit waiting for changes before restart and seems like it should be working but it isnt. I attached a sceengrab to have a better look. View Error
UPDATE #2: I changed the main property in the package.json file to app.js and ran nodemon command and produced this error. A bit of progress?
UPDATE #3: Here is my app.js file for reference
UPDATE #4: I updated my express property verision to include an actual verison number then ran npm install which seemed to be sucessful. I then ran nodemon again. Here are the results
Based on the information you've provided, I would guess you are executing nodemon by just typing the nodemon command without any arguments. This only works if your entry point is named index.js, but in the screenshot you provided it's clear that your entry point is named app.js, so you should be running nodemon with the following command:
nodemon app.js

issues with imports on modularized apollo server

i have a fully working modularized apollo server on nextJS (based on this article https://www.apollographql.com/blog/modularizing-your-graphql-schema-code-d7f71d5ed5f2/ ) basically im doing an Array of my typedefs and merging the resolvers using the merge function from lodash... so far so good... this feed a function makeExecutableSchema and this schema is going to the server... and it works great...
the problem is im trying to move the server outside nextJS... and when i do try to create a new project and yarn init or npm init... install the dependencies, and try to copy paste all my schema files to the new npm init project ALL my imports are messed up, and i start getting all this errors like:
in nextJS i have (this one simply works in nextJS):
import { merge } from "lodash";
in the new node project it says merge couldnt be found in lodash...
or the module is commonJS and it cannot do named imports...
or if i do an import using require it says ReferenceError: require is not defined, i think this one should be due to node expecting to run this on a browser but i have no idea how to specify this wont run on a browser since it's simply a js file which intends to modularized the apollo schema...
i just dont understand why all the import sentences work just fine in Next but when im starting the apollo server in plain node every import is giving so many errors... i have fixed some by adding the extension at the end of the filename im importing (thing that was not necessary in nextJS) or by adding "/index" at the end of the package being imported...
Is there a way to make the imports behave like in Next??? but in a new nodeJS project?
Any help or orientation would be GREATLY appreciated
The answer was:
Use Babel, install with NPM or YARN
"#babel/core"
"#babel/node"
"#babel/preset-env"
add the .babelrc file to the root directory of the project:
{
"presets": ["#babel/preset-env"]
}
to run the main file use a script in package.json like:
"start": "nodemon --exec babel-node index.js"
and this will allow to execute modern JavaScript :D

npm package works locally but modules cannot be resolved when published

I wrote a little npm module. In the main index.js, I am requiring another js file like so:
const Interface = importJsx('./Interface');
interface.js is a functional react component which I am exporting by writing module.exports = Interface; at the end and is in the root folder, just like index.js. When I run node index.js on the command line, everything works. When I publish the module to npm and run npx MY_PACKAGE_NAME, all of a sudden 'Interface' cannot be resolved. What is the workaround for npm's busted file system?

Error running node in cloud9 IDE?

First i got the following error and i couldn't figure yet what is this about:
any idea?
The problem is that you're trying to open an HTML file with Node.js.
Node.js is supposed to run JavaScript code (typically stored in .js files) and it cannot parse your HTML file, which is why you're getting these errors.
You could try executing the files called app.js or server.js with the following commands:
node app.js
or
node server.js
Also, it seems your project (or whatever it is you're working on) has a README.md file which could help to clarify where you're supposed to start.

How to Import nodes modules in react native

hope you're doing well.
I'm new at react native and i'm stuck with a problem while trying to import a node module.
I need to create an app that will get orders from the API of a Wordpress Website with WooCommerce.
I first created a project with the command create-react-native-app picking then npm install. It's creating a structure like this in the project folder named picking:
node_modules
App.js
app.json
App.test.js
etc....
Then I installed the package woocommerce-api with npm install woocommerce-api --save (https://www.npmjs.com/package/woocommerce-api). This package allow me to do request to the WooCommerce API easier.
I want to not put the config to the WooCommerce API in the App.js, so I created a folder src and a folder woocommerce with a file api.js (should I write it with the first letter in uppercase ?) in it and I added import Api from 'picking/src/woocommerce/api'; in my App.js.
So now the structure is
node_modules
src
-- woocommerce
-- api.js
App.js
app.json
App.test.js
etc....
The problem is that I can't achieve to import the WooCommerceAPI module from woocommerce-api, no matter what I set in path to get the module.
There is the file api.js at the moment :
import WooCommerceAPI from '../../woocommerce-api';
var Api = new WooCommerceAPI({
url: 'http://localhost/mysite',
consumerKey: 'ck_xxxxxxxxxxxxxxxxxxxxxxxxxx',
consumerSecret: 'cs_xxxxxxxxxxxxxxxxxxxxxxxxxxx',
wp_api: true,
version: '/wc/v2',
queryStringAuth: true
});
export default Api;
And I get the error :
Unable to resolve module '../../woocommerce-api' from etc ...
I can't find what is the problem and why this is not working. If you could help me on this, it would be very nice.
Have a nice day everyone :)
EDIT: I changed the line for the import to import WooCommerceAPI from 'woocommerce-api'; and I got a new error : Metro Bundler has encountered an internal error, please check your terminal error output for more details, but there is nothing in the terminal except Failed building JavaScript bundle.
EDIT2: I downgrade node from 9.4 to 8.0.0 and restart the project. I got the same error but in the terminal i now get this in yellow/orange : Problem checking node_modules dependencies: Unexpected end of JSON input
Okay, so I find a workaround. In fact, the import is working. For some reason that i don't know, this is the npm package that is not working and make the app crash.
So I removed the package woocommerce-api and I create a file in src/woocommerce called woocommerce-api.js, then I copied the content of this https://github.com/minhcasi/react-native-woocommerce/blob/master/WooCommerceAPI.js that is the same as the one in the npm package and I pasted it in my woocommerce-api.js. I import it in my api.jsfile and "voilĂ " !
Seems to work fine.
As you install woocommerce-api in your project there is no need to place the location like ../../woocommerce-api.
just change ../../woocommerce-api to woocommerce-api and your project should work.

Resources