Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 807
Ben Hadfield Avatar asked Jan 09 '17 17:01

Ben Hadfield


4 Answers

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).

like image 72
Michael P. Bazos Avatar answered Nov 11 '22 05:11

Michael P. Bazos


You can either do

import module from 'path'

or if you just need to import path just do

import 'path'

like image 42
G4bri3l Avatar answered Nov 11 '22 04:11

G4bri3l


If not using typescript

import * as path from 'path'

is the only thing that works for me.

like image 17
Ricardo Rabanales Avatar answered Nov 11 '22 05:11

Ricardo Rabanales


import path from 'path';

As of now, this is the code that's working for me in typescript.

like image 9
Cyril Gupta Avatar answered Nov 11 '22 06:11

Cyril Gupta