相关疑难解决方法(0)

如何在节点中使用es6导入?

我试图在节点中获取es6导入的挂起,并尝试使用此示例中提供的语法:

Cheatsheet链接:https://hackernoon.com/import-export-default-require-commandjs-javascript-nodejs-es6-vs-cheatsheet-different-tutorial-example-5a321738b50f

我正在查看支持表:http://node.green/,但无法找到支持新导入语句的版本(我尝试查找文本import/require)我目前正在运行节点8.1. 2并且还认为,由于cheatsheet是指.js文件,它应该与.js文件一起使用.

当我运行代码时(取自cheatsheet的第一个例子):

import { square, diag } from 'lib';
Run Code Online (Sandbox Code Playgroud)

我收到错误:SyntaxError:意外的令牌导入.

参考lib我正在尝试导入:

//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
    return x * x;
}
export function diag(x, y) {
    return sqrt(square(x) + square(y));
}
Run Code Online (Sandbox Code Playgroud)

我缺少什么,如何让节点识别我的import语句?

node.js es6-modules

240
推荐指数
11
解决办法
22万
查看次数

节点错误:SyntaxError:意外的令牌导入

我不明白出了什么问题.我检查了其他论坛谈论翻译和巴贝尔.我需要做什么?

node -v
v5.5.0
Run Code Online (Sandbox Code Playgroud)

我的代码:

import recast from 'recastai'
Run Code Online (Sandbox Code Playgroud)

和错误

(function (exports, require, module, __filename, __dirname) { import recast from 'module1'
                                                              ^^^^^^

SyntaxError: Unexpected token import
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:387:25)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3
Run Code Online (Sandbox Code Playgroud)

babel node.js

55
推荐指数
3
解决办法
5万
查看次数

(节点:9374)警告:要加载 ES 模块,请设置“type”:“module”

我今天刚开始学习 React。如何在 Visual Studio 的终端中的控制台上消除该错误消息。

(node: 9374)Warning: To load an ES module,
 set "type": "module" in the package.json or use the .mjs extension. 
/Users/nishihaider/workspace-ui/react-todo-app/src/App.js:1
Run Code Online (Sandbox Code Playgroud)
import React from "react";
import "./App.css";

function App() {
  <>
  return (
  <h1>ToDo</h1>
  );
  </>
}

export default App;
Run Code Online (Sandbox Code Playgroud)

javascript reactjs es6-modules

27
推荐指数
5
解决办法
5万
查看次数

不能在模块外使用 import 语句

我将 NextJS 与 typescript、mongo Atlas、mongoose、node 和 express 一起使用。

运行节点页面/服务器时出现以下错误:我已经上传了我的 package.json 文件并添加了 babel

从“快递”导入快递;^^^^^^

语法错误:无法在 Object.Module 的 Module._compile (internal/modules/cjs/loader.js:1122:27) 的 wrapSafe (internal/modules/cjs/loader.js:1072:16) 的模块外使用 import 语句。 _extensions..js (internal/modules/cjs/loader.js:1178:10) at Module.load (internal/modules/cjs/loader.js:1002:32) at Function.Module._load (internal/modules/cjs) /loader.js:901:14) at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12) at internal/main/run_main_module.js:18:47

这是我的 server.js 代码:

import express from 'express';
import { connect, connection } from 'mongoose';
import morgan from 'morgan';
import path from 'path';
const app = express();
const PORT = process.env.PORT || 8080;
//Success
   import routes from './routes/api.tsx';

const MONGODB_URI = …
Run Code Online (Sandbox Code Playgroud)

mongodb node.js express typescript next.js

15
推荐指数
2
解决办法
6万
查看次数

SyntaxError: Cannot use import statement outside a module

I've got an ApolloServer project that's giving me trouble, so I thought I might update it and ran into issues when using the latest Babel. My "index.js" is:

require('dotenv').config()
import {startServer} from './server'
startServer()
Run Code Online (Sandbox Code Playgroud)

And when I run it I get the error "SyntaxError: Cannot use import statement outside a module". First I tried doing things to convince TPTB* that this was a module (with no success). So I changed the "import" to a "require" and this worked.

But now …

javascript node.js babeljs apollo-server

10
推荐指数
15
解决办法
1万
查看次数

从“快递”导入快递;语法错误:意外的标识符

我一直在通过教程制作脚本,但是当我尝试通过 nodemon run index.js 运行 index.js 时,我在指向 import express from 'express' 时遇到语法错误;.

我已经安装了 nodemon,对于 bd,我还运行了 mongo 和 mongod。教程可能已经过时,或者在没有被告知的情况下安装/运行了某些东西。

我的 index.js

import express from 'express';
import dbConfig from './config/db';
const app = express();
dbConfig();
const PORT = process.env.PORT || 3000;
app.listen(PORT, err => {
  if (err) {
    console.error(err);
  }{
    console.log('App listen to port: ${PORT}');
  }
});
Run Code Online (Sandbox Code Playgroud)

包.json:

{
  "name": "meetup-backend",
  "version": "0.0.1",
  "main": "index.js",
  "scripts": {
    "dev": "NODE_ENV=development nodemon dist/index.js",
    "build:watch": "babel -w --out-dir=dist ./src",
    "clean": "rimraf dist",
    "lint": "eslint src", …
Run Code Online (Sandbox Code Playgroud)

import node.js express nodemon

7
推荐指数
0
解决办法
1万
查看次数

为什么不能在带有 VueJS 的 vue.config.js 文件中使用 ES 导入?

为什么ES 导入vue.config.js文件中不起作用?

例如:

import * as path from 'path';
import * as pjson from './package.json';

module.exports = {
  configureWebpack: {
    resolve: {
      alias: {
        '@': path.join(__dirname, '/src'), // Alias @ to /src folder for ES/TS imports
      },
    },
  },
  pwa: {
    name: pjson.title,
    assetsVersion: pjson.version,
  },
};
Run Code Online (Sandbox Code Playgroud)

运行npm run lint命令后出错(使用 vue-cli-service):

\vue.config.js:1
import * as path from 'path';

SyntaxError: Unexpected token *
    at Module._compile (internal/modules/cjs/loader.js:720:23)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:787:10)
    at Module.load (internal/modules/cjs/loader.js:643:32)
    at Function.Module._load (internal/modules/cjs/loader.js:556:12) …
Run Code Online (Sandbox Code Playgroud)

javascript typescript webpack vue.js vue-cli

7
推荐指数
1
解决办法
4015
查看次数

SyntaxError:无法在我的 js 文件中的模块外使用 import 语句

我正在学习 ES6,并且正在学习免费的在线课程。在课程中,我们涵盖了 Promises,所以我有:

import fetch from 'cross-fetch';

const apiUrl = 'https://fcctop100.herokuapp.com/api/fccusers/top/alltime';

function getTop100Campers(){
    fetch(apiUrl)
    .then((r) => r.json())
    .then((json) => {
        console.log(json[0])
    }).catch((error) => {
        console.log('failed');
    });
}

getTop100Campers();
Run Code Online (Sandbox Code Playgroud)

当我运行我的代码(我使用的是 Visual Studio Code)时,我得到:

C:\Program Files\nodejs\node.exe src\ES6-Course.js
c:\Users\j\Documents\Programming\ES6\es6course\src\ES6-Course.js:1
import fetch from 'cross-fetch';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Module._compile (internal/modules/cjs/loader.js:892:18)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:973:10)
    at Module.load (internal/modules/cjs/loader.js:812:32)
    at Function.Module._load (internal/modules/cjs/loader.js:724:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1025:10)
    at internal/main/run_main_module.js:17:11
Run Code Online (Sandbox Code Playgroud)

我错过了什么吗?我查看了 Google 上的错误,所以我安装了 cross-fetch,我还运行了 npm create-react-app es6course 因为我认为我需要运行 React 才能使其工作,但我仍然收到相同的错误消息。

javascript node.js node-modules reactjs es6-promise

6
推荐指数
0
解决办法
2万
查看次数

SyntaxError:导出函数 Js 时出现意外的标记“导出”

我正在尝试从 Js 文件导出函数,但收到Unexpected token错误

const youtubeDownload = require("./youtube/youtube-download"); // export function youtubeDownload 
const twitterDonwload = require("./twitter/twitter-download"); // export function twitterDownload

function download(tweet) {
    if(tweet.in_reply_to_status_id_str == null) return youtubeDownload(tweet);
    if(tweet.in_reply_to_status_id_str != null) return twitterDonwload(tweet);
};

export { download };
Run Code Online (Sandbox Code Playgroud)

此代码返回错误:

export { download };
^^^^^^

SyntaxError: Unexpected token 'export'
Run Code Online (Sandbox Code Playgroud)

javascript export function

5
推荐指数
2
解决办法
5万
查看次数

Nodemon 在尝试使用 es6 模块时崩溃,但在 es5 上运行良好

我正在尝试使用 es6 模块运行服务器,但每次执行时都会崩溃,并且每次使用它时都会出现 es5错误消息

我安装了 babel 并在 .babelrc 文件中有 "preset": ["env"] 但每当我运行它时,我都会遇到“语法错误:无效或意外的标记”。这不是一个特定的项目,这是我遇到这种情况的第三个项目

import http from 'http';
import express from 'express';
import logger from 'morgan';
import bodyParser from 'body-parser';

// setting up express application
const app = express();

const hostName = '127.0.0.1';
const port = 3000;
const server = http.createServer(app);

// logs request to the console
app.use(logger('dev'))

// Parse incoming data requests
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
    extended: false
}));

// making a request to the server
app.get('*', (req, res) => res.status(200).send({
    message: …
Run Code Online (Sandbox Code Playgroud)

node.js express babeljs es6-modules

3
推荐指数
1
解决办法
6436
查看次数

如何正确声明或要求引用 rxjs 库?

使用nodejs和打字稿查看另一个问题的另一个答案,我得到这个异常:

类型错误:interval$.zip 不是函数

我的代码(rxjs2.ts):

{
    var Rx = require('rxjs');

    const interval$ = Rx.interval(1000);
    const items$ = Rx.from([1,2,3]);

    const itemsOverTime$ = interval$.zip(items$).repeat();

    itemsOverTime$.subscribe(([time, val]) => {
    console.log(val);
    // 1
    // 2
    // 3
    // 1
    // 2
    // 3
    });
}
Run Code Online (Sandbox Code Playgroud)

从 VSCode 控制台运行它,例如:node rxjs2.ts

我的 package-lock.json

{
  "requires": true,
  "lockfileVersion": 1,
  "dependencies": {
    "@types/node": {
      "version": "10.12.2",
      "resolved": "https://registry.npmjs.org/@types/node/-/node-10.12.2.tgz",
      "integrity": "sha512-53ElVDSnZeFUUFIYzI8WLQ25IhWzb6vbddNp8UHlXQyU0ET2RhV5zg0NfubzU7iNMh5bBXb0htCzfvrSVNgzaQ=="
    },
    "rxjs": {
      "version": "6.3.3",
      "resolved": "https://registry.npmjs.org/rxjs/-/rxjs-6.3.3.tgz",
      "integrity": "sha512-JTWmoY9tWCs7zvIk/CvRjhjGaOd+OVBM987mxFo+OW66cGpdKjZcpmc74ES1sB//7Kl/PAe8+wEakuhG4pcgOw==",
      "requires": {
        "tslib": "1.9.3"
      }
    },
    "tslib": { …
Run Code Online (Sandbox Code Playgroud)

require node.js rxjs

2
推荐指数
1
解决办法
4544
查看次数