nodejs导入需要转换 [英] nodejs Import require conversion

查看:133
本文介绍了nodejs导入需要转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此处学习NodeJ.问题是,当我尝试搜索答案时,找不到我想要的东西.可能是因为这太基础了或没有问题.

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.

我正在使用angular2开发nodejs.很自然,我有类似的东西:

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

很明显,当我使用import等时,我的代码无法正常工作.否则,我将不会在此处发布代码.是因为我做错了吗?还是我不走运,以至于该特定模块无法与导入配合使用?当我看到require('some_stuff')的用法示例时,有人可以阐明如何编写正确的导入语句,以便可以使用从npm下载的其他模块吗?

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?

提前谢谢.

所以我尝试了npm install requirejs --save.然后我在上面写了require语句.但是我在包装上得到了404 ...

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

推荐答案

您可以使用 import ,但必须使用babel运行应用程序.

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

您必须将此行添加到您的 package.json 文件

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 文件是您的主文件位置

src/server.js file is your main file location

然后使用以下命令运行文件

and then run file with following command

npm run start

使用import { stuff } from 'module';时 那么您可以直接在程序中使用 stuff().

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

但是当您使用var stuff = require('module');时 那么您需要在程序中执行 stuff.stuff().

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

这篇关于nodejs导入需要转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆