Tips on NodeJs and JavaScript Development (General)
Created on: 26 Oct 21 22:40 +0700 by Son Nguyen Hoang in English
Some notable tips on NodeJs and Web Development using JavaScript
A list of tips & common errors when using NodeJs. This list is, as well as the other, under ongoing updates & fixing. Hope it can help you then!
- Symlink Problem when installing package by npm.
My Scenario
- You have an external drive and init a npm project into it. The problem is the external drive is formatted in FAT32 (Window) format. Thus, when installing a simple package (such as nodemon), the system shows up an error:
npm ERR! 'Error: EPERM: operation not permitted, symlink \'../is-ci/bin.js\' -> \'<PATH>/node_modules/.bin/is-ci\'',
npm ERR! errno: -1,
npm ERR! code: 'EPERM',
npm ERR! syscall: 'symlink',
npm ERR! path: '../is-ci/bin.js',
npm ERR! dest:
npm ERR! '<PATH>/node_modules/.bin/is-ci',
npm ERR! parent: 'FCM-test' }
Solution:
- Add ‘–no-bin-links’ when installing package. Example:
npm install nodemon --save --no-bin-links
- Using NODEMON or any script from local dictionary
My Scenario
- Yes! nodemon is super popular and people love to install it globally. However, my philosophy is decoupling and keep everything seperate. The below snippet in package.json run the module from local dictionary instead.
-
Solution:
{
"name": "test",
"version": "1.0.0",
"description": "",
"main": "server.js",
"dependencies": {
"nodemon": "^2.0.14"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node ./node_modules/nodemon/bin/nodemon.js <*YOUR_SERVER_SCRIPT*>"
},
"author": "",
"license": "ISC"
}
Run npm start
every time you want to boot the nodemon. The script’s name (start) can be changed of course.
- Accessing Function from
export
Let’s say we have the below FunctionA
and FunctionB
exports.FunctionA = function(){
// Do Somthing
}
exports.FunctionB = function(){
FunctionA() // ->>> This is an error.
}
To fix the bug, we modify FunctionB
to be:
exports.FunctionB = function(){
module.exports.FunctionA() // --> Bug has been fixed
}
exports.FunctionA = function(){
// Do Somthing
}