Web3.0加密代币应用报错:无法解析./token.did.js文件
问题描述
开发Web3.0加密代币创建应用时,运行程序出现报错:Module not found: Error: Can't resolve './token.did.js' in '/home/name/ic-projects/token/src/declarations/token'
使用的导入语句:import { token } from "../../../declarations/token"
已尝试参考社区内容修改webpack.config.js和tsconfig.json,但问题未解决。
相关文件代码
token.did
service : { balanceOf: (principal) -> (nat) query; }
webpack.config.js
const path = require("path"); const webpack = require("webpack"); const HtmlWebpackPlugin = require("html-webpack-plugin"); const TerserPlugin = require("terser-webpack-plugin"); const CopyPlugin = require("copy-webpack-plugin"); function initCanisterEnv() { let localCanisters, prodCanisters; try { localCanisters = require(path.resolve( ".dfx", "local", "canister_ids.json" )); } catch (error) { console.log("No local canister_ids.json found. Continuing production"); } try { prodCanisters = require(path.resolve("canister_ids.json")); } catch (error) { console.log("No production canister_ids.json found. Continuing with local"); } const network = process.env.DFX_NETWORK || (process.env.NODE_ENV === "production" ? "ic" : "local"); const canisterConfig = network === "local" ? localCanisters : prodCanisters; return Object.entries(canisterConfig).reduce((prev, current) => { const [canisterName, canisterDetails] = current; prev[canisterName.toUpperCase() + "_CANISTER_ID"] = canisterDetails[network]; return prev; }, {}); } const canisterEnvVariables = initCanisterEnv(); const isDevelopment = process.env.NODE_ENV !== "production"; const frontendDirectory = "token_assets"; const asset_entry = path.join("src", frontendDirectory, "src", "index.html"); module.exports = { target: "web", mode: isDevelopment ? "development" : "production", entry: { index: path.join(__dirname, asset_entry).replace(/\.html$/, ".jsx"), }, devtool: isDevelopment ? "source-map" : false, optimization: { minimize: !isDevelopment, minimizer: [new TerserPlugin()], }, resolve: { extensions: [".js", ".ts", ".jsx", ".tsx"], fallback: { assert: require.resolve("assert/"), buffer: require.resolve("buffer/"), events: require.resolve("events/"), stream: require.resolve("stream-browserify/"), util: require.resolve("util/"), }, }, output: { filename: "index.js", path: path.join(__dirname, "dist", frontendDirectory), }, module: { rules: [ { test: /\.(ts|tsx|jsx)$/, loader: "ts-loader" }, { test: /\.css$/, use: ['style-loader','css-loader'] } ] }, plugins: [ new HtmlWebpackPlugin({ template: path.join(__dirname, asset_entry), cache: false, }), new CopyPlugin({ patterns: [ { from: path.join(__dirname, "src", frontendDirectory, "assets"), to: path.join(__dirname, "dist", frontendDirectory), }, ], }), new webpack.EnvironmentPlugin({ NODE_ENV: "development", ...canisterEnvVariables, }), new webpack.ProvidePlugin({ Buffer: [require.resolve("buffer/"), "Buffer"], process: require.resolve("process/browser"), }), ], devServer: { proxy: { "/api": { target: "http://localhost:8000", changeOrigin: true, pathRewrite: { "^/api": "/api", }, }, }, hot: true, watchFiles: [path.resolve(__dirname, "src", frontendDirectory)], liveReload: true, }, };
tsconfig.json
{ "compilerOptions": { "target": "es2018", "lib": ["ES2018", "DOM"], "allowJs": true, "jsx": "react", }, "include": ["src/**/*"], }
解决方法
这个错误核心是dfx未生成token.did.js声明文件,或是webpack无法识别该文件类型,按以下步骤修复:
重新生成声明文件
执行命令让dfx重新编译并生成did对应的JS声明文件:dfx generate token执行后检查
/home/name/ic-projects/token/src/declarations/token目录,确认是否出现token.did.js和token.did.d.ts文件。更新webpack的文件扩展名配置
在webpack.config.js的resolve.extensions中添加.did.js,确保webpack能识别这类文件:resolve: { extensions: [".js", ".ts", ".jsx", ".tsx", ".did.js"], // 保留原有的fallback等配置 }验证导入路径正确性
检查前端代码文件的位置,确认../../../declarations/token的相对路径是否准确。也可以配置webpack别名简化导入:resolve: { // 保留原有的extensions、fallback配置 alias: { "@declarations": path.resolve(__dirname, "src/declarations") } }之后导入可改为:
import { token } from "@declarations/token";清理缓存并重启服务
删除旧的编译缓存文件,重新部署:rm -rf .dfx dist dfx start --clean dfx deploy
内容的提问来源于stack exchange,提问作者ankit
相关产品推荐
相关产品推荐

