javascript - Treat self as a node module for npm -
i have javascript project released node module. reasons, have source code using relative paths import other files in project:
// <this_module_path>/action/foo.js import execution './execution'; import types '../types';
and using module name root path import other files in project:
// <this_module_path>/action/dosomething.js // using module name import // equals import './helpers.js' in case (in same folder) import executionhelpers 'this-module/action/helpers.js'; // equals import '../types/helpers' in case import typehelpers 'this-module/types/helpers.js';
how can have such file import other project files using module name rather relative paths?
nodejs uses commonjs import javascript moduels. there no clear timeline adding es6 import / export
syntax nodejs. need transpile code using babel commonjs module system before can run on nodejs.
how using commonjs
create separate package
this-module
module. package needs created insidenode_modules
directory of main module. can usingnpm init
command insidenode_modules
directory.inside file, need create javascript file (conventionally called
index.js
, make main script of package.
your package.json
should this:
{ "name": "test", "version": "1.0.0", "description": "", "main": "index.js", }
- in
index.js
can export variables (suchhelpers
,types
) , can import them in main package.
Comments
Post a Comment