无法从crypto

编程入门 行业动态 更新时间:2024-10-28 10:27:02

无法从<a href=https://www.elefans.com/category/jswz/34/1771223.html style=crypto"/>

无法从crypto

我正在开发Udacity的区块链纳米级程序,并且在导入SHA256时遇到了一些我不理解的错误。我确实按照github存储库(npm install)中的建议运行了。

这是我的代码:

/**
 * Step 1: Verify 'crypto-js' module is installed for your project
 * After verifying it is installed, move on to Step 2. No code needs to be written here.
 */

/**
 * Step 2: Import from crypto-js module the sha265 library
 */

// Write your code here
// import sha256 from 'crypto-js/sha256';
import SHA256 from 'crypto-js/sha256'

/**
 * Variables: Do not change variable values to be able to answer the quiz
 */

const data1 = "Blockchain Rock!";
const dataObject = {
    id: 1,
    body: "With Object Works too",
    time: new Date().getTime().toString().slice(0,-3)
};

/**
 * Step 3: Add code to the `generate hash function
 * Function that generate the SHA256 Hash
 * @param {*} obj 
 */

function generateHash(obj) {
    // Write your code here
    console.log(SHA256(JSON.stringify(obj)))
}

console.log(`SHA256 Hash: ${generateHash(data1)}`);
console.log("************************************");
console.log(`SHA256 Hash: ${generateHash(dataObject)}`);

/**
 * Run your application using `node app.js`
 */

这是我的package.json:

{
  "name": "hashing",
  "version": "1.0.0",
  "description": "",
  "main": "app.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "babel": "^6.23.0",
    "crypto-js": "^3.3.0",
    "webpack": "^4.5.0"
  },
  "devDependencies": {
    "babel-cli": "^6.26.0",
    "babel-core": "^6.26.3",
    "babel-preset-es2015": "^6.24.1"
  }
}

[当我尝试运行node app.js时,出现此错误:

/git/nd1309_hashing/app.js:12
import SHA256 from 'crypto-js/sha256'
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at wrapSafe (internal/modules/cjs/loader.js:1072:16)
    at Module._compile (internal/modules/cjs/loader.js:1122:27)
    at Object.Module._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

谁能看到我在做什么错吗?

回答如下:

替换:import SHA256 from 'crypto-js/sha256'

更多推荐

无法从crypto

本文发布于:2024-05-07 03:13:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754175.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:crypto

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!