Coder Social home page Coder Social logo

can't require() in nodejs land about brace HOT 3 CLOSED

thlorenz avatar thlorenz commented on May 25, 2024
can't require() in nodejs land

from brace.

Comments (3)

thlorenz avatar thlorenz commented on May 25, 2024

brace is based on ace which only works in the browser. I doubt that will change, but feel free to file an issue with the ace editor.

from brace.

AndrewRayCode avatar AndrewRayCode commented on May 25, 2024

Woah, hold on!! 😃 I understand Ace is designed to work in the browser. Lots of NPM modules are:

$ node

    > require('jquery')
        [Function]

    > require('three')
        { REVISION: '71', ... }

    > require('react')
        { Children: { map: [Function: mapChildren], ...

There's a few issues here:

1. Fundamentally (more below), a module in npm should be requirable in node land. That's one of the reasons why I and others choose to use this library instead of copying Ace Javascript files into our project.

2. If the project errors at require() time, that means that simply requiring the project has side effects. Side effects are generally considered Bad™ when they happen when a function runs, and worse when they occur simply from including the presence of code.

3. Strangely, Ace already seems to guard against this. So I'm not yet sure what's causing the window error. If it's something obvious I'll add a bug to the Ace editor as well.

4. In these modern JS times we need libraries to be required in Node for server side rendering (from any library, but namely React). I don't mean that they need to render server side, but they need to be required() without error. Take this structure for example:

let editor = require('Editor');
let homepage = require('Homepage');
...
render() {
    if( page === 'homepage' ) return homepage;
    if( page === 'editor' ) return editor;

Even if we're on a page that doesn't render the editor, the editor will still be required. If we try to execute this on the server to render our app to server, it will fail because simply requiring the editor will block load.

5. In these modern JS times, we can test our components in isolated unit tests in NodeJS. Imagine I'm testing the homepage above. I can't, because simply requiring the homepage will require the editor. I can't even load the homepage first to stub out the editor, because I can't require it(!!).

Now I suppose the question centers around point 1. Whose responsibility is it to make the package "work" without side effects in Node? If you're maintaining this project simply by copying over the source files exactly 1:1 from Ace, then it's understandably more tricky because your modified code could get out of sync with the source. If you're making any modifications the code already, then it seems obvious to make minor changes to make it work in node.

FYI, if anyone encounters this issue, one way around this (which isn't great) is to do something like this:

let isClient = typeof window !== "undefined";
let ace = isClient ? require('brace') : function() {};

from brace.

AndrewRayCode avatar AndrewRayCode commented on May 25, 2024

The best friends club:

from brace.

Related Issues (20)

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.