创建简单的NODEJS项目

  • 起因

想简单测试CryptoJS,但是又不想带一个大项目启动,只是想用命令行简单测试下面的test1.js而已

import CryptoJS from 'crypto-js';

const loginInfo = "这是一段我想加密的信息 2!@#$@!#$"
const passPhras = "^@#$%#$!#&#$%&"


var encrypted = CryptoJS.DES.encrypt(loginInfo, passPhras)
console.log("加密:", encrypted.toString())

var decrypted = CryptoJS.DES.decrypt(encrypted, passPhras)
console.log("解密:", decrypted.toString())
  • 创建一最简化的项目
mkdir test1
cd test1
npm init

这样项目就创建好了,然后命令行运行:

npm install crypto-js

组件安装好了,但是运行出错

node test1.js

出现下面的错误
(node:4276) Warning: To load an ES module, set "type": "module" in the package.json or use the .mjs extension.
(Use `node --trace-warnings ...` to show where the warning was created)
/root/test/node1/test1.js:1
import CryptoJS from 'crypto-js';
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

参考:https://bobbyhadz.com/blog/javascript-syntaxerror-cannot-use-import-statement-outside-module,修改一下package.json,加上"type": "module"这行

{
  "name": "node1",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "type": "module",
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "crypto-js": "^4.1.1"
  }
}

再运行

node test1.js

一切顺利

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值