Note About Front End Development, Vue, React, Java Spring, Maven Etc.

Finally find a place cool enough to host a blog

View on GitHub
30 September 2018

A short understanding about Node&NPM Module or Package loaded

by libai8723

介绍Nodejs和Npm Module是什么,其实后来回想的时候发现,js的module定义真的好乱,其实直接从TypeScript来学可能也是一个不错的思路。

A short understanding about Node&NPM Module or Package loaded

1. Background

where i trying to figure out how to custom a mendix widget, i knew that i have to have a basic knowledge about Webpack.

so i start to read the Get Start of webpack which i think is written pretty awsome.

becasue i have very little knowledge about the front-end development, neither with the tool-chains.

so i’m very confused with the first 3 commands, actually the last 2 command.

the command is as follows:

mkdir webpack-demo && cd webpack-demo
npm init -y
npm install webpack webpack-cli --save-dev
  1. the first one is easy to say that my want a webpack-demo dir as the base-dir for our project.
  2. the send one is tricky, so i read the npm init command, and find that -y means keep everything to yes link, and use the default config, or i will be asked a lot of questions about the project. but the package.json file generated by the npm init command, is totally worth to read link
  3. so let’s see the 3rd command says that we will install webpack, webpack-cli, with a –save-dev option. So this makes me even more curious, because the days before today, all i know with npm is to install package/module with -g option. which i think is more cool, because it save a lot disk space, and i will use the global installed package as cache, so that it is more convenient to develop new project with the existing lib. but when i see the –save-dev, it makes me thinking more
  4. so first read the doc about npm install more carefully finding out install can have more options, such as -P –save-prod, -D –save-dev, -O –save-optional each with different meaning.
  5. save-prod, meaning that the package will be used for production environment, so npm will list the package as the dependency in the package.json file
  6. while with –save-dev, meaning that the package is only used for development work, so when you do the pack work, the dependency will not count this package
  7. here is the npm install doc

as i was reading this get start tutorial, i find in the lodash example, the lodash lib seem so cool, that i want to try the lib in node.

so i typed the following command as usual:

npm install -g lodash

and npm works great, and it told me that the package in added.

when i try to type:

node
> var _ = require('lodash');

it print some error stacktrace saying that can not find the lodash module, this make me feel frustrated. so i googled a lot and finally find that node does not encourage us to use the global installed package

so here is the facts i want to say:

modules loading from the global folders

origin link:

If the NODE_PATH environment variable is set to a colon-delimited list of absolute paths, then Node.js will search those paths for modules if they are not found elsewhere.

On Windows, NODE_PATH is delimited by semicolons (;) instead of colons.

NODE_PATH was originally created to support loading modules from varying paths before the current module resolution algorithm was frozen.

NODE_PATH is still supported, but is less necessary now that the Node.js ecosystem has settled on a convention for locating dependent modules. Sometimes deployments that rely on NODE_PATH show surprising behavior when people are unaware that NODE_PATH must be set. Sometimes a module’s dependencies change, causing a different version (or even a different module) to be loaded as the NODE_PATH is searched.

Additionally, Node.js will search in the following list of GLOBAL_FOLDERS:

1: $HOME/.node_modules 2: $HOME/.node_libraries 3: $PREFIX/lib/node Where $HOME is the user’s home directory, and $PREFIX is Node.js’s configured node_prefix.

These are mostly for historic reasons.

It is strongly encouraged to place dependencies in the local node_modules folder. These will be loaded faster, and more reliably.

so the final conclusion is that use the local lib, so now i feel the network connection is such important than ever before.

3. how to let node find the modules in global folds

in windows setup the NODE_PATH environment variable, so all my global modules in ‘C:\Users\Administrator\AppData\Roaming\npm\node_modules’

so i set the variable to this, and it totally works, feels cool.

4. TODO

still have to find the baisc usage of webpack ,and try to do the very first mendix widget using the full-calendar.

if it works, carry on to add more features

tags: Node.js