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 import
s, 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 install
ed one. Thx.