Better way to require.extensions with Node.js

Asked
Active3 hr before
Viewed126 times

10 Answers

better
90%

Why does light accelerate instantaneously to c, while no other phenomena do it? ,So final answer - I would assume that nobody at Node will break Babel, and if they do, they will probably give another solution for the same problem. I would not hesitate to use it!,I use node-hook to stub all .scss calls in my tests.

Pretag
 Pretag team - issue, fix, solve, resolve
88%

require.resolve(request[, options]) require.resolve.paths(request) ,require.resolve.paths(request),request <string> The module path to resolve.

load more v
72%

Doing require extensions correctly is essential, because:,What are require.extensions for? what is the purpose?,Got it, thanks, yeah last time I checked ts-node uses require.extensions

require.extenstions['.js'] = function(module, filename) {
   var content = fs.readFileSync(filename, 'utf8');
   module._compile(internalModule.stripBOM(content), filename);
}
load more v
65%

This aims to solve a problem within node.js. If you look at node documentation you will see that it has been deprecated which mean that we shouldn't use it anymore. However the module is locked so it will be around and never changed.,The filename is passed as well so that it can be used to do things if you know the filename, Better Error messages, inserting information at transpile time and so on.,If you would like create a module we would like the follow the same direction of so many other loading system

This makes sense interns of production and published module, however for development this is pretty needed feature. Especially with so many compilers and features that are available.

    require.extensions[".js"] = function(module, filename) {
       //compile file          
    }
load more v
75%

Add new extensions to require() , Add new extensions to require() ,You can add new extensions to require() by extending require.extensions.

For a XML example:

// Add .xml for require()
require.extensions['.xml'] = (module, filename) => {
   const fs = require('fs')
   const xml2js = require('xml2js')

   module.exports = (callback) => {
      // Read required file.
      fs.readFile(filename, 'utf8', (err, data) => {
         if (err) {
            callback(err)
            return
         }
         // Parse it.
         xml2js.parseString(data, (err, result) => {
            callback(null, result)
         })
      })
   }
}
load more v
40%

However, since Node allows many ways to require a file (for example, with a relative path or a pre-configured path), before we can load the content of a file into the memory we need to find the absolute location of that file.,If Node can’t find find-me.js in any of these paths, it will throw a “cannot find module error.”,If, for example, the find-me.js file was under a lib folder instead of the node_modules folder, we can require it with:

Pretag
 Pretag team - issue, fix, solve, resolve
22%

In this article we will talk about the most important concepts to develop native extensions for Node.js. Later, I will show how to build our first native extension with a practical example. And finally, I will leave some links to read more about it.,Later, we need to create our binding.gyp file that will contain our native extension configuration:,And that’s it. We just run our native extension.

Pretag
 Pretag team - issue, fix, solve, resolve
60%

Or you can use the full path:,/home/myname/myprojects/myapp/node_modules/mymodule.js,/home/myname/node_modules/mymodule.js

var http = require('http');
var spawn = require('child_process').spawn;
require('./mymodule');
require('/home/myname/myapp/mymodule.js');
/home/myname / myprojects / myapp
var circle = require('./circle.js');
delete require.cache('./circle.js');
load more v
48%

All subsequent files required by node with the extensions .es6, .es, .jsx, .mjs, and .js will be transformed by Babel.,One of the ways you can use Babel is through the require hook. The require hook will bind itself to node's require and automatically compile files on the fly. This is equivalent to CoffeeScript's coffee-script/register., You must include the polyfill separately when using features that require it, like generators.

npm install @babel / core @babel / register--save - dev
load more v
23%

With all tools installed, webpack can now be configured. By convention, a webpack.config.js file contains the configuration to instruct webpack to bundle your extension. The sample configuration below is for VS Code extensions and should provide a good starting point:,Migrating an existing extension to use webpack is easy and similar to the getting started guide above. A real world sample that adopted webpack is the VS Code's References view through this pull request.,This configuration for running tests is the same for non-webpacked extensions. There is no reason to webpack unit tests because they are not part of the published portion of an extension.

esbuild is a fast bundler that's simple to configure. To acquire esbuild, open the terminal and type:

npm i--save - dev esbuild
load more v

Other "better-undefined" queries related to "Better way to require.extensions with Node.js"