D
D
demsp2019-04-15 23:23:48
Node.js
demsp, 2019-04-15 23:23:48

Require() in Node.js (exports or this)?

The article Understanding require() in Node.js shows the difference between exports and this .
This example

var PI = 3.14
this.area = function ( r ) {
 return PI * r * r
}
this.circumference = function ( r ) {
 return 2 * PI * r
}

should display circle.PI but I get undefined
Also I get an error when calling the test.js file related to the-super-tiny-compiler

var t=new require('./test')

mistake

(function (exports, require, module, __filename, __dirname) { const {

PS Tell me, what is the difference between node and nodejs
Already asked this question
Can I demolish both
sudo apt-get purge node
sudo apt-get purge nodejs

and put again
curl -sL https://deb.nodesource.com/setup | sudo bash -
sudo apt-get update && sudo apt-get install -y node
sudo apt-get update && sudo apt-get install -y nodejs

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lynn "Coffee Man", 2019-04-16
@demsp

You install some kind of fossil node 0.10
Read the source https://github.com/nodesource/distributions#debinstall and you only need to install nodejs
Well, or even use some kind of nvm .
The 2011 article is somehow not very good either, and it is all described by simple identities:

// в контексте модулей
this === exports // true
this === module.exports // true

as well as the fact that this in the context of a module does not need to be used https://nodejs.org/api/deprecations.html#deprecati...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question