我正在嘗試創建自己的 NodeJS 包,該包使用粉筆 輸出彩色字串,但我得到空字串作為輸出(當我node test.js在終端中運行時)我嘗試在線搜索但找不到任何原因。有誰知道可能導致此錯誤的原因是什么?
index.js 檔案
import chalk from 'chalk';
import pkg from 'koa/lib/request';
const { charset } = pkg;
const fontColors = ['red','green','yellow','blue','magenta','cyan',];
export const colourfulLog = (string) => {
const colorString = string.split(' ')
.map(word => {
const randclrindex = Math.floor(Math.random() * fontColors.length);
const randColor = fontColors[randclrindex];
return chalk[randColor][word];
})
.join(' ');
console.log(colorString);
}
test.js 檔案
import { colourfulLog } from './index.js';
colourfulLog(' Test the colorful log package');
包.json 檔案
{
"name": "colorful-log-ax",
"version": "1.0.0",
"description": "first package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "AT",
"license": "ISC",
"dependencies": {
"chalk": "^5.0.1"
},
"type": "module"
}
uj5u.com熱心網友回復:
您的 index.js 檔案中有錯誤:
return chalk[randColor][word];
您正在使用方括號動態指定方法: chalk[randColor] 就像說 chalk.randColor,那么您必須word像這樣應用它:
return chalk[randColor](word);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477287.html
標籤:javascript 节点.js 细绳 粉笔
上一篇:將字串轉換為顫動中的日期串列
