1

I'm learning node but have never used ES6/javascript/TypeScript before, so please bear with me...

I'm trying to write nodejs application in plain ES6. From the following example (taken from here),

import fs from 'fs';
export default class Animal {

  constructor(name){
    this.name = name ;
  }

  print(){
    console.log('Name is :'+ this.name);
  }
}

import Animal from 'path/to/Animal.js';

We can see there are two kinds of imports, the ES6 way of require. One is without path (import fs from 'fs';), and the other is with path.

So my first confusion is, from here it says,

For compatibility with CommonJS and in preparation for future features, relative paths that don’t start with ./ or ../ are not allowed (in ES6):

// Not allowed:
import * as foo from 'foo.mjs';
import * as foo from 'lib/foo.mjs';

So is import fs from 'fs' right or wrong?

The plain ES6 nodejs application I'm trying to write is based on a npm module, but because it is almost updated daily, I'm pulling from its git instead doing

npm install mydepmod

This in turn makes its sample code of which starts with

import { mydepmod } from 'mydepmod'

not working for me. The error I'm getting is,

module.js:557
    throw err;
    ^

Error: Cannot find module 'mydepmod'
    at Function.Module._resolveFilename (module.js:555:15)
    at Function.Module._load (module.js:482:25)
    at Module.require (module.js:604:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/path/to/example/the-test.js:19:21)
    at Module._compile (module.js:660:30)
    at Object.Module._extensions..js (module.js:671:10)
    at Module.load (module.js:573:32)
    at tryModuleLoad (module.js:513:12)
    at Function.Module._load (module.js:505:3)

so my last question is how to make it works?

BTW, I tried its docker installing and running from docker (which starts with import { mydepmod } from 'mydepmod') work without any problem, so I assume if I do npm install mydepmod, it should work as well.

All in all, how can I make my git pulled dependent module works just like a npm installed one. Thx.

xpt
  • 20,363
  • 37
  • 127
  • 216
  • Simple to say,without path way is `node module` and other is your project file ,here are more information about `import`. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import – Cr. Jan 31 '18 at 03:38
  • Yeah, I'm asking how to use node module just as node module, even thought I didn't do `npm install`, but `git get`. See my OP for updated error message, thx. @Cr. – xpt Jan 31 '18 at 03:48
  • without path and if isn't nodejs core module, import will find `node_module` floder under the project folder, if does not have this module, it will find `node_module` floder under the first level directory until the root directory. If haven't ,throw not found error. – Cr. Jan 31 '18 at 03:57
  • Ah, gotya @Cr., having read your answer several times, I think I know the answer to my question (which I will try it out next). thx! – xpt Jan 31 '18 at 04:15

1 Answers1

1

When you import using relative path with prefix './' then it loads module from that file, you can give full path like '/path/to/module'.

When you import any module without any path, then node look for that module

  • in node_modules folder, either in that directory, or in any parent directory where package.json exists
  • if it fails to load module from that directory, then node try to load that module from global node_modules folder which is in users directory (e.g., /usr/lib/node_modules/).
  • If node fails to load module at this stage, then it will throw error saying Can not find module.

Here is a detailed description about how require works. Require and Import/Export are similar, this article gives difference between require and import/export.

For difference between local node_modules and global node_modules, here is a discussion.

Location of global node_modules is different in different os environment. According to this answer:

On Unix systems they are normally placed in /usr/local/lib/node or /usr/local/lib/node_modules when installed globally. If you set the NODE_PATH environment variable to this path, the modules can be found by node.

Windows XP - %USERPROFILE%\Application Data\npm\node_modules Windows 7, 8 and 10 - %AppData%\npm\node_modules

Laxmikant Dange
  • 7,606
  • 6
  • 40
  • 65
  • 1
    Thx! I'll get back to you once I've made my git pulled dependent module works just like a npm installed one. BTW, is (my added) `/usr/lib/node_modules/` what you meant by _"is in users directory"_? – xpt Jan 31 '18 at 04:29
  • Laxmikant: have you noticed @Cr's answer, _"import will find node_module floder under the project folder, if does not have this module, it will find node_module floder under the first level directory until the root directory. "_, which is different than yours? Can you double check which saying is more correct please? My [new reading](https://nodejs.org/docs/latest/api/modules.html#modules_all_together) seems to indicate his. Thx! – xpt Feb 03 '18 at 05:20