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

  1. create separate package this-module module. package needs created inside node_modules directory of main module. can using npm init command inside node_modules directory.

  2. 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", } 
  1. in index.js can export variables (such helpers , types) , can import them in main package.

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -