Setup:
I have NodeJS installed on my system (C) drive on a Windows 8 x64 machine. I have QUnit installed globally via npm:
npm install qunit -g
The problem:
If I attempt to reference QUnit with:
var q = require('qunit');
while running NodeJS from any directory on the C drive, everything works as expected. However, when I run it from my projects directory which resides on my secondary E drive, Node cannot find my globally installed package:
Error: Cannot find module 'qunit'
at Function.Module._resolveFilename (module.js:338:15)
at Function.Module._load (module.js:280:25)
at Module.require (module.js:362:17)
at require (module.js:378:17)
at repl:1:9
at REPLServer.self.eval (repl.js:109:21)
at rli.on.self.bufferedCmd (repl.js:258:20)
at REPLServer.self.eval (repl.js:116:5)
at Interface.<anonymous> (repl.js:248:12)
at Interface.EventEmitter.emit (events.js:96:17)
Is there some other configuration I'm missing to be able to use globally installed packages while running NodeJS from my secondary drive? Is this just unsupported? I'd like to not have to locally install them and check them into source control, but it is a backup option if global packages do not work from secondary drives.
https://npmjs.org/doc/folders.html
Local install (default): puts stuff in ./node_modules of the current package root.
Global install (with -g): puts stuff in /usr/local or wherever node is installed.
Install it locally if you're going to require() it.
Install it globally if you're going to run it on the command line.
If you need both, then install it in both places, or use npm link.
To do this you may want to look into creating a package.json file. Which should include all dependencies for a given project. Then you should be able to run npm install to install all the given dependencies for your project.
There's a related answer here about creating a package.json file.
Related
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
I have downloaded an angular project from angular.io tutorial when i run this project
it shows error
Could not find module "#angular-devkit/build-angular" from "C:\Users\asus\angulario".
Error: Could not find module "#angular-devkit/build-angular" from "C:\Users\asus\angulario"
Could not find module "#angular-devkit/build-angular" from "C:\\Users\\asus\\angulario".
Error: Could not find module "#angular-devkit/build-angular" from "C:\\Users\\asus\\angulario".
at Object.resolve (C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\#angular-devkit\core\node\resolve.js:141:11)
at Observable.rxjs_1.Observable [as _subscribe] (C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\#angular-devkit\architect\src\architect.js:132:40)
at Observable._trySubscribe (C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\rxjs\internal\Observable.js:44:25)
at Observable.subscribe (C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\rxjs\internal\Observable.js:30:22)
at C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\rxjs\internal\Observable.js:99:19
at new Promise (<anonymous>)
at Observable.toPromise (C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\node_modules\rxjs\internal\Observable.js:97:16)
at ServeCommand.initialize (C:\Users\asus\AppData\Roaming\npm\node_modules\#angular\cli\models\architect-command.js:88:94)
at process._tickCallback (internal/process/next_tick.js:68:7)
at Function.Module.runMain (internal/modules/cjs/loader.js:744:11)
project live example https://stackblitz.com/angular/bvypgkmlmdv
Download link https://angular.io/generated/zips/toh-pt3/toh-pt3.zip
You haven't installed the node modules.
Kindly run the following command npm install and run the application.
You need to install the node modules first.
For that you need to run npm i in the root folder of your downloaded app
(and you need also to have npm installed)
First install NodeJS to your computer, then install npm package manager using global key,
npm i npm -g
And afer that you can install npm packages depending your package.json file as answered previous message.
npm i
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
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!
I used the following commands to install a mean stack and create an app:
> sudo npm install -g meanio#latest // Get the mean cmdline
> mean init myApp // create your first app
> cd myApp && npm install // Install dependencies
> grunt // Launch mean
but when I get to the final grunt command I get the error:
module.js:333
throw err;
^ Error: Cannot find module 'coffee-script'
at Function.Module._resolveFilename (module.js:331:15)
at Function.Module._load (module.js:273:25)
at Module.require (module.js:357:17)
at require (module.js:373:17)
at Object.<anonymous> (/home/eddie/ResFour/node_modules/grunt/lib/grunt.js:16:1)
I've tried various solutions on stackoverflow to no avail:
NodeJS - setting up mean.io cannot find module errors
Cannot find module 'coffee-script'
I also cleared npm cache and reinstalled a few times.
Remove node_modules directory.
npm cache clean to empty downloaded temporary dependencies, just in case.
npm install again, and better not to interrupt it while running.
These solved most of these problems for me.
This worked for me:
npm install --save-dev coffee-script
node -v # v0.10.31
I had the same problem, and in the end of the log I was receiving such a message:
...
http 200 http://registry.npmjs.org/-/all
Killed
The problem was the lack of enough memory in my VM. One solution was to use swap is mentioned here.