Giter Site home page Giter Site logo

require-from-esm's Introduction

require-from-esm

⚠️ Warning note ⚠ ️

  1. This is very experimental
  2. Node.js' latest version (12.0.0) is required
  3. You should run Node.js with the --experimental-modules flag
  4. You probably should run Node.js with the --es-module-specifier-resolution=node flag

require-from-esm is a small package with literally few lines aiming to provide interoperability between CJS modules and ES modules.
To be more specific, require-from-esm let you require CJS modules with the old friend Node.js global require() from ES modules.
With ES modules on Node.js we can use almost every feature from ECMA modules (still experimental).

Example:

import require from 'require-from-esm';
import path from 'path';
const mymodule = require('../../mymodule.cjs'); // You should use `../../` for local modules *

console.log(path.normalize('/foo/bar//baz/asdf/quux/..'));
console.log(mymodule);

For local modules you should use require(../../MYMODULE) instead of require(./MYMODULE).
We don't have __dirname and __pathname globals when using ES modules.
No solution was found to fix it until now. PRs are welcome.

How to use

  1. Be sure you're using Node.js >12.0.0
  2. The flags, you should use the flags: --experimental-modules and --es-module-specifier-resolution=node
  3. You can use npm scripts to run your app, something like this:
  "scripts": {
    "start": "node --experimental-modules --es-module-specifier-resolution=node index.js"
  },
  1. Make sure you have the "type": "module" on your package.json

Further reading:
https://medium.com/@nodejs/announcing-a-new-experimental-modules-1be8d2d6c2ff
http://2ality.com/2019/04/nodejs-esm-impl.html
https://scotch.io/tutorials/new-ecmascript-modules-in-node-v12#toc-new-features-in-node-v12

require-from-esm's People

Stargazers

Roman avatar

Watchers

Jaydson Gomes avatar James Cloos avatar  avatar

Forkers

doubleedesign

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.