admin管理员组文章数量:1134001
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?
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?
Share Improve this question edited Jan 9, 2017 at 17:15 Bergi 663k158 gold badges1k silver badges1.5k bronze badges asked Jan 9, 2017 at 17:11 Ben HadfieldBen Hadfield 1,6512 gold badges11 silver badges6 bronze badges 2- What transpiler are you using? Have you tried it? – Bergi Commented Jan 9, 2017 at 17:16
- 1 Possible duplicate of NodeJS plans to support import/export es6 (es2015) modules – Sam Hanley Commented Jan 9, 2017 at 17:16
5 Answers
Reset to default 301For 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).
If not using typescript
import * as path from 'path'
is the only thing that works for me.
import path from 'path';
As of now, this is the code that's working for me in typescript.
You can either do
import module from 'path'
or if you just need to import path
just do
import 'path'
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.
本文标签: javascriptCan you import node39s path module using import path from 39path39Stack Overflow
版权声明:本文标题:javascript - Can you import node's path module using import path from 'path' - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736785483a1952834.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论