How to run node in VSCode properly? - javascript

$ node test.js
internal/modules/cjs/loader.js:883
throw err;
^
I tried all the ways (adding/checking PATH route for node js, restarting, using another files etc) and couldn't find why my node doesn't run properly. When I'm typing node -v it returns me version, so it is installed, I go to VSCode and open my any folder. Then in JS file when I'm typing for instance node app.js it throws me error that I wrote above.
Any idea what should I try else?

Seems like node modules are not properly installed.
Try these steps and let me know:
delete node_modules
delete package-lock.json file
npm install
npm start
should be good to go.

Related

Getting Node Module Error (Cannot find module)

I am learning the Automated tests by using selenium web driver + Javascript and node.js.
Everything is working fine when I ran that script.js from the Visual Studio code terminal(by using node main.js)
Problem
I want to schedule this script in the scheduler which automatically tests the login functionality. But when I try to run it from the task scheduler then it gives Error: Cannot find module
Does anybody know how to get rid of this.
To fix Cannot find module errors, install the modules properly by running a npm install command in the appropriate directory as your project's app. ... or delete the node_modules folder and package-lock. json file and re-install it again using the npm install command.
rm -rf node_modules package-lock.json

How to launch node script from anywhere in the commandline like Angular ng?

I am working on a node module which does some stuff, i would like to run the command like Angular's ng command (it has to be compatible with Windows and Linux i have tried inspecting the angular cli module and make some modifications to my node module but it still doesn't seem to work)
Here is what i have done:
I have installed the node module globally via npm install -g <nodemodule>
The starting script has the correct shabang which is: #!/usr/bin/env node this is to make the command run in Linux without specifing node
I have removed the extension from the file which runs the command and it is something like this:
#!/usr/bin/env node
var cli = require('./cli/cli);
I am sure the script works, cause if i go in the directory and launch the script there, it works.
If you already added #!/usr/bin/env node and set chmod +x yourCommand.js You only need to add:
"bin": {
"your-command": "/path-to-your-command"
}
To the root of your module's package.json, here you can check a good guide on how to do it.

react-native, bundling failed

I am new to react-native and have found an error that makes absolutely no sense to me.
I am trying to include a calendar library: https://github.com/wix/react-native-calendars
I have included the dependency for that library in my package.json file and run npm install
The project was running fine and as expected. I have simply added one line of code to the project:
import { Calendar, CalendarList, Agenda } from 'react-native-calendars';
Nothing else. However now I am getting a red error message when the app loads on device and this is what my console is saying:
error: bundling failed: Error: While trying to resolve module xdate
from file
/Users/Alex/Documents/workspace/mobile/node_modules/react-native-calendars/src/index.js,
the package
/Users/Alex/Documents/workspace/mobile/node_modules/xdate/package.json
was successfully found. However, this package itself specifies a
main module field that could not be resolved
(/Users/Alex/Documents/workspace/mobile/node_modules/xdate/src/xdate.js.
Indeed, none of these files exist:
*
/Users/Alex/Documents/workspace/mobile/node_modules/xdate/src/xdate.js(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)
*
/Users/Alex/Documents/workspace/mobile/node_modules/xdate/src/xdate.js/index(.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)
at ResolutionRequest.resolveDependency (/Users/Alex/Documents/workspace/mobile/node_modules/metro/src/node-haste/DependencyGraph/ResolutionRequest.js:103:15)
at DependencyGraph.resolveDependency (/Users/Alex/Documents/workspace/mobile/node_modules/metro/src/node-haste/DependencyGraph.js:272:4579)
at dependencies.map.relativePath (/Users/Alex/Documents/workspace/mobile/node_modules/metro/src/DeltaBundler/traverseDependencies.js:376:19)
at Array.map ()
at resolveDependencies (/Users/Alex/Documents/workspace/mobile/node_modules/metro/src/DeltaBundler/traverseDependencies.js:374:16)
at /Users/Alex/Documents/workspace/mobile/node_modules/metro/src/DeltaBundler/traverseDependencies.js:212:33
at Generator.next ()
at step (/Users/Alex/Documents/workspace/mobile/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:313)
at /Users/Alex/Documents/workspace/mobile/node_modules/metro/src/DeltaBundler/traverseDependencies.js:297:473
at
However I have checked in the directory and in fact /Users/Alex/Documents/workspace/mobile/node_modules/xdate/src/xdate.js does exist. However I do not see any of this part: (.native||.ios.js|.native.js|.js|.ios.json|.native.json|.json)`
As to how /Users/Alex/Documents/workspace/mobile/node_modules/xdate/src/xdate.js/index can exist, that doesn't make sense because xdate.js is not a directory.
Any help?
It Usually happens if you try to install a module while your packager is open.
Try to delete node_modules folder and close the packager.
Then reinstall everything by npm install in your project directory.
First close your JavaScript bundler (Metro bundler in my case) and restart the application. This will create fresh dependency graph. This should solve the issue.
Just restart the bundler - no need to delete the Node Modules folder
I came across this issue when adding and using react-native-elements when using VS Code and Android Studio emulator on Linux Mint
In VS Code I had a terminal running Expo with Metro Bundler via 'npm start' command. After installing the react-native-elements in the folder (using 'npm install --save react-native-elements') and adding the import into the .js file I got the "However, this package itself specifies a main module field that could not be resolved" error.
In my case, this just involved a ^C in the terminal session to stop the Metro bundler and then running 'npm start' again. There was no need to delete the node modules folder at all.
If you're still getting the error even after deleting the ./node_modules folder and running npm install again, try doing the install with the --update-binary flag, i.e npm install --update-binary. This should clear out and rebuild any previously built packages on your machine.
Please don't delete the node modules folder.
The bundler has least to deal with node modules folder.
Instead close all the terminals. If using VS-code close that also.
Close all the local host server ports and if possible restart the system.
This might solve the issue.
If anyone have same issue, and remove node_module not working. Then you can try remove all node_module, package lock and using yarn to install node_module instead npm. Hope this help
None of the solutions above helped me except for restarting my MacBook. Wasted 30 minutes trying to fix this issue as I couldn't believe restarting my computer could be a solution.

Error: Cannot find module 'curl' at Function.Module._resolveFilename (module.js:489:15)

I installed node.js with the windows installer. The folder structure formed is C:\Program Files\nodejs\node_modules\npm\node_modules. The last node_modules has all the module folders I guess.
I use the node.js command prompt to execute my js script. When the script is on Desktop, it executes all fine. When I shift my script to another folder in my C: it gives the error. I don't want to give the absolute path as it will have to be changed again and again. Anyways, what path should I give as absolute to make it just work
Error: Cannot find module 'curl'
at Function.Module._resolveFilename (module.js:489:15)
I have gone through the resolutions mentioned in other discussions. Have made the NODE_PATH environment variable. Have edited PATH variable to make it C:\Program Files\nodejs and not C:\Program Files\nodejs\. Have executed npm install -g npm and nothing seems to work.
Sorry for the trouble, if anyone has already started looking into it. I found the solution. In the node.js command prompt, in the project folder I executed the following 'npm install curl' which downloaded the curl packages into a node_modules folder in the folder which has my script. I anyways wonder, then how it worked when I had placed my project on Desktop. It seems that the way node.js finds modules has to be checked. FYI - I have not set the NODE_PATH and the path still has C:\Program Files\nodejs\ after I freshly installed it. Thank you!

My node based coffee script not running (Error %1 is not a valid application.)

I am working with this repository https://github.com/indiejoseph/Radar
When I run the coffee script located in the web folder of repositor.
It gives a following error:
https://drive.google.com/file/d/0BwjNIlr7qO3_YTltQzRhNkdmX2M/view
Hmm, this looks like it might be related to 32/64 bit issues of the node dependencies, see here and here for some similar issues.
Just running through the list of some obvious things:
reinstall all the dependencies: delete node_modules folder, npm cache clear and npm install.
Have you installed the arduino drivers?
Which version of node do you have installed?

Categories

Resources