Reputation: 1651
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?
Upvotes: 155
Views: 222103
Reputation: 13723
import path from 'path';
As of now, this is the code that's working for me in typescript.
Upvotes: 21
Reputation: 459
If not using typescript
import * as path from 'path'
is the only thing that works for me.
Upvotes: 35
Reputation: 28239
For people trying to import path
in a TypeScript file, and ending up here:
Be sure to have node types installed:
npm install --save-dev @types/node
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).
Upvotes: 303
Reputation: 36319
If the version of nodejs you're using supports the ES 6 features, then yes. Otherwise not. Most of the older versions (pre 6.x if memory serves but you should check for your version) required the --harmony flag in order to do this, the latest releases include it natively.
For this reason, and because it works in all versions, most online resources still use the require syntax.
Upvotes: 2
Reputation: 5146
You can either do
import module from 'path'
or if you just need to import path
just do
import 'path'
Upvotes: 15