Giter Club home page Giter Club logo

es-main's Introduction

es-main

Test if an ES module is run directly with Node.js. Acts as a replacement for require.main.

use

import esMain from 'es-main';

if (esMain(import.meta)) {
  // Module run directly.
}

why?

It can be useful to have a module that is both imported from other modules and run directly. With CommonJS, it is possible to have a top-level condition that checks if a script run directly like this:

if (require.main === module) {
  // Do something special.
}

With ES modules in Node.js, require.main is not available. Other alternatives like process.mainModule and module.parent are also not defined for ES modules. In the future, there may be an alternative way to do this check (e.g. import.meta.main or a special main export). Until then, this package provides a workaround.

es-main's People

Contributors

dependabot[bot] avatar tschaub avatar xamgore avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

es-main's Issues

esMain(import.meta) pops an error

Hey,

I am getting the following error with esMain(import.meta) line:

iMac:api ninjablade$ sls deploy
Serverless: Bundling with Webpack...

ERROR in /Volumes/Work/Work/Workspace/+Purpose/Backend/services/api/user.js 105:17
Module parse failed: Unexpected token (105:17)
File was processed with these loaders:
 * ../../babel-loader/lib/index.js
 * ../node_modules/eslint-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| });
| 
> if (esMain(import.meta)) {
|   get(mock);
| }

ERROR in /Volumes/Work/Work/Workspace/+Purpose/Backend/services/api/user.js 105:17
Module parse failed: Unexpected token (105:17)
File was processed with these loaders:
 * ../../babel-loader/lib/index.js
 * ../node_modules/eslint-loader/dist/cjs.js
You may need an additional loader to handle the result of these loaders.
| });
| 
> if (esMain(import.meta)) {
|   get(mock);
| }
 
 Error ---------------------------------------------------
 
  Error: Webpack compilation error, see stats above
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/serverless-webpack/lib/compile.js:32:15
      at arrayEach (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/lodash/lodash.js:530:11)
      at Function.forEach (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/lodash/lodash.js:9410:14)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/serverless-webpack/lib/compile.js:29:7
      at tryCatcher (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/promise.js:729:18)
      at Promise._fulfill (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/promise.js:673:18)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/bluebird/js/release/nodeback.js:42:21
      at finalCallback (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:257:39)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:273:13
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at onCompiled (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:271:21)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:681:15
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:678:31
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1423:35
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1414:32
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1409:36
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1405:32
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:6:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at Compilation.seal (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1342:27)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:675:18
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1261:4
      at AsyncSeriesHook.eval [as callAsync] (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:24:1)
      at AsyncSeriesHook.lazyCompileHook (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/Hook.js:154:20)
      at Compilation.finish (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1253:28)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compiler.js:672:17
      at eval (eval at create (/Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/tapable/lib/HookCodeFactory.js:33:10), <anonymous>:11:1)
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1185:12
      at /Volumes/Work/Work/Workspace/+Purpose/Backend/node_modules/webpack/lib/Compilation.js:1097:9
      at processTicksAndRejections (internal/process/task_queues.js:75:11)
 
     For debugging logs, run again after setting the "SLS_DEBUG=*" environment variable.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment Information ---------------------------
     Operating System:          darwin
     Node Version:              14.16.1
     Framework Version:         2.52.1
     Plugin Version:            5.4.3
     SDK Version:               4.2.3
     Components Version:        3.14.2

The strange thing is that this is only happening with webpack bundling.. when I run it locally, it doesn't have any error.

Add TypeScript types

Consider adding a main.d.ts with:

export default function (meta: ImportMeta): boolean;

Workaround:

declare module 'es-main' {
  export default function (meta: ImportMeta): boolean;
}

Does not support main module resolve

This lib does not work for the package.json below:

{
  "name": "foo",
  "version": "1.2.3",
  "description": "Package with custom main field",
  "main": "foo.js",
  "type": "module",
  "scripts": {
    "start": "node ."
  }
}

When running npm start on the project above, the argv[1] returns the directory where the project lives, but without the main module at the end.

Since this is supported by the package.json spec, I would like to add support for it here too.

usage should be updated

Example in readme

A library available for browser, also provide cli.
When using bundler,

import esMain from 'es-main';

// library code

if (esMain(import.meta)) {
  // cli only code
}

Expected

// library code

Actual

// es-main source code

// library code

if (esMain(import.meta)) {
  // cli only code
}

Pull

#15

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.