Search code examples
node.jssystemjs

How to tell systemjs to ignore an import


This is for the systemjs library, not systemjs-builder

I might have a bit of an edge case here. But I'm trying to setup my frontend web application to run it's unit tests in the command line. My setup currently builds the typescript to the register format and loads is imported into the site as per their specifications. The issue I am currently facing is that one of my dependencies reflect-metadata has a node auto detect system that makes a require('crypto') call, which is a native node module (not available in npm). The error I am receiving is:

Error: ENOENT: no such file or directory, open '/Users/path/to/project/node_modules/crypto.js'

There are lots of things at play here I understand, but the behavior I am after is for systemjs to completely ignore the require('crypto') call and allow it to pass through completely unchanged as node will correctly load this at runtime.

Due to the multi-module enterprise application we are creating I don't really have the freedom to export the typescript to commonjs due to sub-dependencies receiving the Systemjs version rather then a temp commonjs build. (The only way to do this would be to include the commonjs version in our private npm dist build, which is far from ideal)

Here is the current config I am using for system as I have been attempting to get system to ignore the crypto library require. I've left this in the exact state it was when the above error was generated, comments and all so you could see what I've tried.

System.config({
  defaultJSExtensions: true,
  map: {
    'angular2': 'node_modules/angular2',
    'reflect-metadata': 'node_modules/reflect-metadata/Reflect',
    'crypto': 'node_modules/crypto'
  }
  // packages: {
  //   'reflect-metadata': {
  //     format: 'global',
  //     exports: 'Reflect',
  //     defaultExtension: false,
  //     meta: {
  //       '*': {
  //         defaultExtension: false
  //       }
  //     }
  //   }
  // }
  // meta:{
  //   'reflect-metadata/Reflect':{
  //     format: 'global',
  //     exports: 'Reflect',
  //     defaultExtension: false
  //   }
  // }
});

Solution

  • To ignore a require you can map it to the @empty module within your SystemJS config file. you can do this by by adding System.config({ map: { 'crypto': '@empty' } })