How does my node.js server know how to find the express.js file?
In my server.js file I have a requirement for express:
var express = require('express');
var app = express();
My server.js file is in the app folder while express.js is in the app\node_modules\express\lib folder:
express.js is here in the lib directory
Does node.js automatically know to look in node_modules for dependencies? There is no direct enumeration of the path anywhere - I don't see it in the code.
This is a sample project from scotch.io and I'm trying to pick it apart and learn. I'm pretty new to the MEAN stack and I'm trying to understand this at a basic level.
FYI this is node v 4.5.0
The comments already cover the main answer, but I'll try to break it out here for a slightly more complete overview of the subject (not all encompassing). The short version is that node looks in node's core (built in) packages, then your project's node_modules path for modules (packages, but we generally require modules) matching the name. 1
Package Installation, Saving, and Location
Node uses npm to install dependencies, which can be either a "dependency" or "devDependency"; the latter is used for development concerns that shouldn't be required for just normally using the module. We save these to our project's package.json using the flag --save or --save-dev (such as npm install express --save). 2
The package.json file is what sits at the root of your project tree (the project's folder/directory) and contains the dependency information, along with other information. 3 This is defines a "package". When a person publishes a package to npmjs, the (default) registry for packages to install via npm, they should include a well formed package.json which lists its own dependencies, files to include, and what is the "main" file to start with, should it be used in a require statement.
The dependencies you install by running npm install after cloning down the project's repository, will install the packages specified in the package.json, into the node_modules path in the root of your project (where you should be running the install command from).
Side Note
After checking the GitHub repo listed by the article you referenced, you appear to have created each of the files inside the app/ directory.
Require-ing
The use of a require statement in node is in the style of CommonJS and for node, looks first (after core packages) in the node_modules/ path or, if you specify a relative path to a folder or file, you can get it directly. Path relative means that it starts with ./ for a prefix to the current working directory (that the file is executing from), ../ for the directory above the current one, etc. There are other ways of handling paths, such as as the path module that is built into node 4. For example, the following are valid:
require('./some-other.js') require in that file's module.exports, from the some-other.js file 5, in the current, relative path
require('./some.json') will bring in JSON formatted content from a .json file 6, in the current, relative path
require('./routes') will/can also bring in exported content from the routes/ path (directory), which will by default start with the directory's index.js, should it exist 7
That last method is a nice way to be able to bring in a more complex requirement, without keeping everything in a single, overly busy file.
Let's consider the possible sources of modules:
Core modules provided by Node.js.
Package modules from doing npm install. These modules are stored in node_modules folder which is usually located in the root of the project.
Modules in any other location in your project (Usually modules created by you)
If you require modules without any prefix e.g require('a_module'), the core modules are searched first, If it's not found, the package modules are searched next. See the Node.js docs here
If you require modules with prefix / or ./, e.g require('/another_module') , require(./another_module), another_module is considered relative to the location of the requiring file. This is how you would require modules in any other location.
Check Node.js modules docs for further reading.
Related
I have downloaded/installed a node_module (npm package)...
I have played around with the internal files to suit my needs inside the node_modules folder.
It is the most convenient for me to just use it as a node_module, but I'm worried that the next time I update npm packages it might update and erase all of the changes I have made.
What is the best way to make sure only that particular package should not be updated?
Thank you very much
Make the version of that package specific in package.json file.
For example you manually added 3.0.0 version of express in dependencies or devDependencies, change the entry of express as:
"express":"3.0.0"
You can store your own/modified packages either locally (e.g. in a common place outside of the project) or in the cloud (e.g. GitHub) and then reference the package via the file path or repository URL respectively.
Check here:
local paths
Git URLs as Dependencies
I am trying to package my electron app, which is basically two npm directories one inside other. The internal npm directory is my electron app and it makes use of the outer directory scripts. My internal package.json file which is associated with electron app is where I define my electron-packager scripts. I have a question here, I have given my source file for the package as '.' which means present directory, does this also consider the files in the outer directory? I tried this and my app is working fine for now, but I doubt if somewhere something can go wrong if I am not doing this properly. If this is not the proper way how can I tell electron-packager to consider files outside of the current directory? or Does it automatically consider all the required files and scripts irrespective of their path while packaging?
It automatically considers all the files present in the current directory which includes the package.json file. Also I found 'Bolt' useful while working with multiple npm packages.
https://www.npmjs.com/package/bolt
I have an NPM package that can be used with the browser. But in order to use it in the browser, I pre-package it using Webpack and put the browserified code in the /dist directory.
Normally, I don't feel it's necessary to include the /dist directory when publishing to NPM, unless someone wants to use the browser version instead of the Node.js version (most of my customers will be using my lib for Node.js not for front-end).
The dist is a huge directory (all the project's code, plus NPM deps) and I want to save people the disk space and install time.
Should I just create a separate package for the browser code, or is there some flag I can use for conditionally including the dist directory when people install my package?
I believe it's better to create two separate packages.
I m actually studying continuous integration and I m actually facing a (little) problem when dealing with the build sequence of the process.
Actually, I have an application that has the following directory at the root of the project:
src
doc
dist
tests
node_modules
My question is : when I m at the building step (building the last artifact to put in production, after testing process) should I copy the node_modules directory inside of the dist folder ? So this dist folder could work in standalone (with minification etc... etc...) and so I have only to deploy this folder in my prod environnement ?
How can I move only "dependencies" modules and not "devDependencies" modules ?
You don't need to copy anything, because Node when you require a module within your Node app, it will search for node_modules in current directory, and if the dependency is not found, it will try to search in its parent and so on.
Check out how Node looks for a package here: http://mycodesmells.com/post/node-basics-looking-for-package/
If you don't want to have development dependencies in your production environment, you can install only non-dev ones:
npm install --production
Source: https://docs.npmjs.com/cli/install
this is probably a silly question but am new to Meteor and struggling a bit. I want to build a stellar app that tweets when you get stellar. There is a nice Javascript API stellar-lib that works on node, but im unsure how to access the modules in Meteor...
I'm also building an app with Meteor and stellar-lib and have found two options:
1) You can manually copy over the built stellar-lib.js or stellar-lib-min.js from the build/ directory from either the github repo or the node_modules folder you installed it to when you ran the npm install command.
If you do this, you will have to copy the .js file to client/compatibility, otherwise stellar-lib will not work (note: this means you can only use Stellar on the client).
2) If you need it on the server, you can also have browserify wrap stellar-lib for you, then copy the output to lib/ in your Meteor app's root directory. I did this in my repo here with gulp.
Here's a short explanation to how 2) works:
.gulp is where I'll install my NPM modules where they will be ignored by the Meteor build environment (because the folder is hidden).
In deps.js, I require the modules I would want to use in my Meteor app as I would if I was using them in a traditional node.js app. package.json defines the modules I'll install with NPM and the gulpfile.js describes a build task that will resolve my require statements and output a single deps.js file that includes my dependencies to my Meteor app's lib/ folder.