最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - nodejs Import require conversion - Stack Overflow

programmeradmin7浏览0评论

Learning NodeJs here. Problem is when I tried to search for answers, I am not finding what I am looking for. Probably because this is too basic or non-issue.

I am working on nodejs with angular2. So naturally, I have things like:

import { stuff } from 'some_module'

But I am trying to work with a package that has usage example of:

var stuff = require('some_module')

Obviously, my code didn't work when I use import etc. else I wouldn't be posting here. Is it because I am doing something wrong? Or am I out of luck such that this particular module doesn't work with import? Can someone shed some light on how to write proper import statements when I see usage sample of require('some_stuff'), so I can use other modules I download from npm?

thanks in advance.

EDIT: So I tried npm install requirejs --save. Then I wrote the require statement above. But I am getting a 404 on the package...

Learning NodeJs here. Problem is when I tried to search for answers, I am not finding what I am looking for. Probably because this is too basic or non-issue.

I am working on nodejs with angular2. So naturally, I have things like:

import { stuff } from 'some_module'

But I am trying to work with a package that has usage example of:

var stuff = require('some_module')

Obviously, my code didn't work when I use import etc. else I wouldn't be posting here. Is it because I am doing something wrong? Or am I out of luck such that this particular module doesn't work with import? Can someone shed some light on how to write proper import statements when I see usage sample of require('some_stuff'), so I can use other modules I download from npm?

thanks in advance.

EDIT: So I tried npm install requirejs --save. Then I wrote the require statement above. But I am getting a 404 on the package...

Share Improve this question edited Feb 23, 2017 at 4:46 user691856 asked Feb 23, 2017 at 4:14 user691856user691856 1851 gold badge1 silver badge12 bronze badges 8
  • You mean you want to use require in node, Right? – Sandesh K Commented Feb 23, 2017 at 4:18
  • I guess my confusion is whether there is an equivalent to require for import? Or should I just write "require" in my angular2 ponents? – user691856 Commented Feb 23, 2017 at 4:25
  • Basically both things do same job for you... What you're using in angular in newer syntax from ES6 and in node you're using older syntax from ES5... (ES5 and ES6 are version name for JS)... My opinion is you should go with newer syntax (You can get it with new version of node i.e. 6.9 or use babel on older version) – Sandesh K Commented Feb 23, 2017 at 4:44
  • ah good, but my import statement isn't working for me. I am getting 404 on finding the package. specifically, this is googleapis package. I tried getting requirejs and use it like their usage sample but it still didn't work for me. – user691856 Commented Feb 23, 2017 at 4:48
  • so I am doing import {google} from 'googleapis' – user691856 Commented Feb 23, 2017 at 4:51
 |  Show 3 more ments

2 Answers 2

Reset to default 9

You can use import but you have to run your app with babel.

you have to add this line to your package.json file

"scripts": {
    "start": "NODE_ENV=production node_modules/.bin/babel-node --presets 'es2015' src/server.js",  
    "build": "NODE_ENV=production node_modules/.bin/webpack -p"
  },
  "dependencies": {
    "babel-cli": "^6.11.4",
    "babel-core": "^6.13.2",
    "babel-loader": "^6.2.5",
    "babel-preset-es2015": "^6.13.2"
  },
  "devDependencies": {
    "http-server": "^0.9.0",
    "webpack": "^1.13.2",
    "webpack-dev-server": "^1.14.1"
  }

src/server.js file is your main file location

and then run file with following mand

npm run start

when you use import { stuff } from 'module'; then you can directly use stuff() in your program.

but when you use var stuff = require('module'); then you need to do stuff.stuff() in your program.

It's interesting that the original syntax

var stuff = require('some_module')

is not working for you. If your app was scaffolded from Angular CLI then it should support both imports and require statements out of the box.

for example, I'm using MSR in an Angular 2 ponent like this:

var MediaStreamRecorder = require('msr');
发布评论

评论列表(0)

  1. 暂无评论