javascript - 需要读取本地文件的 npm 包?
问题描述
我正在构建一个 ES 模块,该模块需要通过命令行在浏览器和 Node.js 中运行,并且它需要读取包含包中数据的文件。想象一个英语词典包,其中english_words.txt
有一个常规单词列表,index.js
看起来像这样
import fs from 'fs'
import path from 'path'
import { fileURLToPath } from 'url'
const __dirname = path.dirname(fileURLToPath(import.meta.url)) // There's no __dirname in ES modules
const dictionaryFile = path.resolve(__dirname, 'english_words.txt')
const englishWords = fs.readFileSync(dictionaryFile).split('\n')
export const isEnglishWord = word => englishWords.includes(word)
我的问题是我不知道如何以在浏览器中也可以工作的方式读取 txt 文件。如果有人将我的包与 JS 构建器/捆绑器一起使用,那将如何工作?
解决方案
推荐阅读
- r - 如何判断一个函数调用了哪些函数?像 traceback() 一样不需要错误?
- javascript - 将模块导出到对象,shell 脚本
- angular - 带有事件发射器的 Angular 动态 Html 模板组件
- ios - PWA iOS 获取 sms: 标签以打开 iMessage
- c# - 从单词中识别和提取(显示)错误
- oracle - connect_by_filtering Oracle 查询结果
- java - java动态数组创建
- javascript - 如何保留 onclick 函数的结果
- java - 将结果存储在 jList 和 arraylist 中
- wso2 - WSO2IS 5.7.0 上的 customTokenIssuer 扩展点未按预期工作