- 起因
想简单测试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"
}
}
再运行文章来源:https://www.toymoban.com/news/detail-538676.html
node test1.js
一切顺利文章来源地址https://www.toymoban.com/news/detail-538676.html
到了这里,关于创建简单的NODEJS项目的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!