Can you import node's path module using import path from 'path'

Ben Hadfield picture Ben Hadfield · Jan 9, 2017 · Viewed 56.4k times · Source

I prefer using the import x from 'y' syntax, but all I've seen online is const path = require('path').

Is there a way to import the path module using this syntax?

Answer

Michael P. Bazos picture Michael P. Bazos · Jul 20, 2017

For people trying to import path in a TypeScript file, and ending up here:

  1. Be sure to have node types installed:

    npm install --save-dev @types/node
    
  2. Import path symbol:

    import * as path from 'path';
    

Note: @types/* are automatically included for compilation, providing you use typescript version 2.0 or above, and provided you don't override the types property in compiler options file (tsconfig.json).