我正在学习 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 才能使其工作,但我仍然收到相同的错误消息。
在 Nodejs 中,语句import Obj from 'module_1'与const Obj = require('module_1')? 这是 Obj 应该是构造函数的时候。我想知道,因为使用 import 语句似乎对我不起作用,所以我想知道是否有其他的编写方式。
之间有什么区别:
import()ES6+ 技术的动态以及
require()在 AMD 技术(requireJS 库)中?
我知道这是require/ importES6的事情。我有一个使用的项目import,文件全部.js,基于此,我需要的只是添加"type":"module"到最近的package.json中。这是我的 package.json ,与 server.js 和 env.js 处于同一顶层:
{
"name": "my project",
"version": "1.0.0",
"description": "many .js",
"main": "server.js",
"type":"module",
"directories": {
"test": "test"
},
"dependencies":{...},
"devDependencies":{...},
"scripts":{...},
"author":"John Doe"
}
Run Code Online (Sandbox Code Playgroud)
服务器.js
import express from 'express';
import 'babel-polyfill';
import cors from 'cors';
import env from './env';
Run Code Online (Sandbox Code Playgroud)
还有
Internal/process/esm_loader.js:74 internalBinding('errors').triggerUncaughtException( ^ 错误 [ERR_MODULE_NOT_FOUND]: 找不到从 c:\project\server.js 导入的模块 'c:\project\env' 您是否打算导入../env.js?
节点是14.9.0,使用nvm。
启动.json
{
"version": "0.2.0",
"configurations": [
{
"type": "node", …Run Code Online (Sandbox Code Playgroud) 我没有找到在 nodejs 中更喜欢 es6 导入而不是 require 的好理由。我阅读了这个 SO 答案,但它列出了无法回答我的问题的导入/要求之间的区别。
马上:
import对于那些没有exports.default这些功能的模块有很多问题import- 像动态导入(和/或它需要esm模块才能工作或棘手的解决方案)的支持有限所以,你能帮我理解:
而且,作为模块维护者,迁移代码库是否仅与浏览器内执行有关?
我在一个反应原生项目中遇到了问题.我正在尝试执行一般要求文件,我导出所有模块.之后,我想只需要我的"require.js"文件,以避免在每个文件中需要('../../ ModuleName')这样的调用.
我有4个文件:
index.ios.js
/app/home.js
/app/MyView.js
/app/require.js
Run Code Online (Sandbox Code Playgroud)
require.js:
module.exports = {
Home: require('./home'),
MyView: require('./MyView')
}
Run Code Online (Sandbox Code Playgroud)
在index.ios.js(他的模块Home和MyView正确获取importet)
'use strict';
var React = require('react-native');
var {
AppRegistry,
StyleSheet,
Text,
View,
} = React;
var {
Home,
MyView
} = require('./app/require');
class Test_require extends React.Component {
render() {
return(
<Home />
);
}
}
AppRegistry.registerComponent('Test_require', () => Test_require);
Run Code Online (Sandbox Code Playgroud)
Home.js(模块MyView没有得到importet)
'use strict';
var React = require('react-native');
var {
View,
Text
} = React;
var {
MyView
} = require('./require');
class Home extends React.Component …Run Code Online (Sandbox Code Playgroud) 我使用的是最新的nodejs版本,但在使用es6模块导入时仍然出错.
我还可以在博客上找到该节点尚不支持es6导入模块. https://nodesource.com/blog/es-modules-and-node-js-hard-choices/ - 没有JavaScript运行时当前支持ES模块.我是javascript和node的新手,有人使用esj模块导入nodejs,而不会将代码转换为低js版本.
我对尝试在浏览器代码中使用 geofire-common 感到有点困惑。
一开始,我对 NodeJS 进行了成功的尝试,它使用了Google 文档中geofire-common引用的内容,其代码如下所示:
import { createRequire } from 'module'; // <------ trick to get a require function
const require = createRequire(import.meta.url);
const { initializeApp, cert } = require('firebase-admin/app');
const { getFirestore } = require('firebase-admin/firestore');
const serviceAccount = require('./serviceAccountKey.json');
initializeApp({
credential: cert(serviceAccount),
});
const db = getFirestore();
const geofire = require('geofire-common'); //<---------- geofire-common
Run Code Online (Sandbox Code Playgroud)
然后,我想从浏览器端完成这项工作......我偶然发现了这个例子。在这个例子中,我可以看到一个“require”函数,所以我猜它与老式的 commonJS 模块有关:
const geofire = require('geofire-common');
Run Code Online (Sandbox Code Playgroud)
Require不是标准 JavaScript API 的一部分,应该是 NodeJS 特定的(这有点奇怪,因为我认为这个例子是浏览器代码),所以我尝试使用 ES6 导入。
import {geofire} from …Run Code Online (Sandbox Code Playgroud) 我目前正在学习 React,并且我有一个使用 webpack、babel 和 React 的应用程序。看来 React 有两种使用 或 来编写它的required方法import。似乎还有更多关于使用的文档import。如何更改我的堆栈以使用导入版本?
我正在尝试将 ES6 imorts 与 babel 和 Node.js 一起使用
import "./utils.js";
log("something"); //throw an error : log is not defined
Run Code Online (Sandbox Code Playgroud)
我的utils.js样子是这样的:
function log(... params){
console.log(... params);
}
log("module utils executed");
//Is executed and display "module utils executed" in the console.
Run Code Online (Sandbox Code Playgroud)
我也尝试过使用export function log(... params)andexport default log(... params)但它不起作用。
所以我不明白这是如何运作的......
编辑:
我知道另一种导入方式是import utils from "./utils.js"
但这不是我想要的。我希望能够使用log()而不用模块变量名作为前缀。就像这篇博文一样
node.js ×8
javascript ×7
ecmascript-6 ×4
reactjs ×3
amd ×1
commonjs ×1
es6-promise ×1
geofire ×1
import ×1
ios ×1
node-modules ×1
react-native ×1
requirejs ×1
vite ×1