Error encountered when importing modules in Node.js - javascript

When I install modules with command npm install <module_name> -g, I'm able to import module in Node.js command line via require(),
but when I execute node <path/name.js> to run my code,I encountered MODULE_NOT_FOUND error

When you run your script nodejs search inside your project to find the desired module, if it is not found it will not search for the global.
So, you need to install the module in your project dir, by running the command
npm install <module>
This will create the node_modules folder, and your script will found the module.
You can understanding better about module are handled reading the official node documentation: https://nodejs.org/api/modules.html#modules_accessing_the_main_module

Related

Some of the globally installed npm packages are not working

I have npm installed globally and whenever I run npm -v to check its version, it displays it correctly which is currently 7.13.0.
I also have heroku-cli installed globally but whenever I run heroku --version it says:
C:\Users\MyName\AppData\Roaming\npm/node_modules/node/bin/node: line 1: This: command not found
Inside C:\Users\MyName\AppData\Roaming\npm I have heroku and npm both installed globally but whenever I run a heroku command it fails to run. I am unable to figure out the issue.
Below I have attached the screenshot of my globally installed packages directory.
Maybe your npm's directory path configuration might have some error.
You can modify path by following steps.
Open or create a ~/.profile file and add your npm path
export PATH=C:\Users\MyName\AppData\Roaming\npm/node_modules/node/bin
On the command line, update your system variables:
source ~/.profile
Or else, I recommend changing the npm's default directory which is explained on npm official docs.(https://docs.npmjs.com/resolving-eacces-permissions-errors-when-installing-packages-globally). I also had similar issue and followed same instructions.

'mocha' is not recognized as an internal or external command - mocha not getting automatically installed with supertest

I need to unit-test my rest api written in NodeJS. For this, I need to use supertest npm package. I was following this tutorial article for implementing the unit test functionality.
Now, in this article it is mentioned that mocha is already included in supertest itself, so there's no need to install it separately. So, I only install supertest using - npm install --save-dev supertest.
But, when I add this line in my package.json - "test": "mocha", and run npm test, it gives error saying 'mocha' is not recognized as an internal or external command. I need to run npm install --save-dev mocha to make it run successfully. But why do I need to include mocha separately and if that's how it is supposed to run, why is it not mentioned in this article or on npm homepage of supertest?
I went to Supertest page on npmjs and checked the dependencies. Mocha is listed as a dev dependency. it means Mocha was used while creating supertest, but is not required for supertest to function.
When I work on Node.js project, I install certain packages as dev dependencies. These could be packages for testing (Mocha) or linting (Standard). What it means is when the project is deployed, it will not need these packages to function.
Also, you can use npm list command to see which modules are installed. I just installed supertest and ran npm list. This is structure of supertest :
As you can see Mocha is not listed
Further, ran npm ls mocha and output is:
$ npm ls mocha
nodetest#1.0.0 E:\Nodetest
-- (empty)
and when I ran npm ls supertest, output is:
$ npm ls supertest
nodetest#1.0.0 E:\Nodetest
-- supertest#3.3.0
Why sis the article say mocha is available? may be in an earlier version of supertest it was included as a dependency

Install ReactJS without internet [duplicate]

I have a project which I will have to deploy to client Windows systems where it will not be possible to connect to internet. I currently have a folder in D:\NODE which contains node.exe and npm.cmd and a node_modules folder. To be able to run node from command line I have added D:\NODE to PATH variable.
I can have most of the modules installed locally inside node_modules of my project. However there's one - node-windows - which needs to be installed globally to work.
Following suggestion below I went to node-windows (installed globally) and packaged it up (npm pack), which created a tarball. I have then copied that file with my project and tried to install it on the test machine globally like this: npm install -g node-windows-0.1.5.tgz
I can see that it got installed in the global directory. However when I try to run the command which uses this module it complains that it cannot find it: Error: Cannot find module 'node-windows'
When I list the modules (npm list -g) it is clearly there in the list...
What do you think? And thank you.
You can install packages on a system without internet connection by packing them using built-in functionality in npm. This way, the node modules will be installed properly.
Create a package.json.
In your package.json, list all the modules you need under bundledDependencies (docs on npm).
Run npm install to install your node files before packing.
Create a tarball with npm pack.
Copy the tarball over to the machine without internet connection.
Install the modules with npm install <filename>.
Update
Regarding your comments, it looks like your globally installed node modules isn't found.
Try using the npm link command (docs on npm link):
cd yourAppFolder
npm link node-windows
1 - In system with internet access install module with this command:
npm install [module name]
2 - go to %userprofile%\AppData\Roaming\npm\node_modules[module name]\
(e.g C:\Users\janson\AppData\Roaming\npm\node_modules\grunt-cli)
3 - run npm pack
4 - this should result in a [module name]-x.y.z.tgz file
5 - run npm i -g [module name]-x.y.z.tgz in offline system

Aurelia Babel unknown option base.modules error

I downloaded the skeleton project from the Aurelia docs and tried starting it up by using the command:
gulp watch
However, I get the following error:
[09:48:13] gulp-notify: [Error running Gulp] Error: [BABEL] <filelocation>: Unknown option: base.modules
Is there a fix for this?
did you follow the full project setup instructions on the docs page or in the project's readme.md?
Running The App
To run the app, follow these steps.
Ensure that NodeJS is installed. This provides the platform on which the build tooling runs.
From the project folder, execute the following command:
npm install
Ensure that Gulp is installed globally. If you need to install it, use the following command:
npm install -g gulp
Ensure that jspm is installed globally. If you need to install it, use the following command:
npm install -g jspm
Install the client-side dependencies with jspm:
jspm install -y
To run the app, execute the following command:
gulp watch
Browse to http://localhost:9000 to see the app. You can make changes in the code found under src and the
browser should auto-refresh itself as you save files.
I read over one step, which was the npm install command which you have to execute from the project folder. I got some errors that the CL.exe file was missing and after some Googling, I found out that was due to me not having Visual Studio 2013 not installed, which kept the utf-8 validator package from not installing.

Unexpected NodeJS and NPM behavior

Does npm install only install packages in the directory you run it from? Because that's my current experience with it. At first, I ran npm install xml in a command prompt at C:/Users/ME. Running require("xml"); in a node instance that was run from C:/Users/ME works, and running npm ls lists the xml package...
But if I move to any other directory, neither of them do.
Is this the expected behavior (doesn't sound right), a Windows thing, or am I missing some kind of install option?
Yes, this is normal behavior.
npm install (in package directory, no arguments):
Install the dependencies in the local node_modules folder.
In global mode (ie, with -g or --global appended to the command), it
installs the current package context (ie, the current working
directory) as a global package.
By default, npm install will install all modules listed as
dependencies. With the --production flag (or when the NODE_ENV
environment variable is set to production), npm will not install
modules listed in devDependencies.
NPM Install Docs
npm installs modules either below the current directory (in a node_modules sub-directory) or if you use the -g flag when running it, it install modules in the global location which is determined via OS and configuration and then node.js will be able to find the module no matter where it is being loaded from.
So, this will install a module globally:
npm install -g xml
When using require(), if you want require to look in the current directory to load a module, then you have to do it like require("./module");, otherwise it will not look for the modules install in the current directory.
You need to do npm install -g xml. This would install the packages globally.
'-g' represents global. Then when you check on other directories, you could list the same package
Note the way you install node is important as well!
You can install node with:
chocolatey (https://chocolatey.org/packages?q=nodejs)
nodeJS website (https://nodejs.org/)
*Knowing where to set your user variables is essential know how for windows developers! The reason I mention this is that the list of chocolatey, and node USER : PATH variables gets so large at one point nodejs and with it npm will just stop working. You need to work within your env. variables in one scenario where that happens. (Can typically happen with multiple versions of SQL Server installed)
If you want an excellent free, offline-available resource on node and npm I suggest devdocs (http://www.devdocs.io) and as well GentlNode's cheat sheets (https://gentlenode.com/journal/cheatsheet) *GentleNode not avail. offline FYI.
There's some key information you haven't received yet. See below:
npm install xml - installs "xml" package and required modules to the local folder you are in when you run the command. Files exist for you to work on in this folder alone(and thus commands only work in this folder) and "xml" cannot be called in cmd/terminal outside of this folder(without rerunning with "-g" param) "xml" is NOT written to package.json file. When and if project is uploaded to github will upload in full if you don't add node_modules to your .gitignore file.
npm install -g xml - install "xml" to your global npm folder, which is referenced to your "user environment variables" (what allows you to call specific functions, methods, and executables from within the command line) Files are NOT installed to your project folder, not recorded in "package.json" file. Will NOT upload anything to github.
npm install --save xml - using "--save" allows you to "install" to this folder, but also to write the package you've included into your project's "package.json" file. Here, your aim is to distribute your software and you want as little files on github/source control as possible that npm knows how to install like jQuery, XML, body-parser, etc. etc.
You would typically use a regular "npm install XML" command for a lightweight, rapid release mini-tool to support a key feature or process improvement you just want to keep on your local machine. In addition, you could npm install "packages" to a folder, and use that as a backup "node folder" mapped to your environment variables.
You would typically use a global "npm install -g XML" command for something like middleware, express, node updates installed VIA npm, and other processes you wish to have access to in cmd/terminal from "anywhere." Once this is established for your core development packages in node, you often just use the proceeding command, read the explanation for more detail.
Lastly, you would use "npm install --save" to write into the "package.json" file the required module(s) you've installed to your project. This would be for those who have worked heavily in node and have used "npm install -g" to already globally install their most common core nodeJS. This will write to the package.json the packages and versions being leveraged in your software currently so that your end-user can download the lightweight version and use npm package management to install all of the module dependencies so the app works correctly on other people's computers.
*Note, this leads to the last command "npm install" which you can run in a given folder where you've downloaded the file directly from github.
Hope that helps a bit more for the given facets of installation using npm!

Categories