Truffle 迁移因缺少函数而失败,但它存在于 node_modules
Truffle migrate fails due to missing function, but it exists in node_modules
尝试 运行 truffle 迁移时,出现以下错误:
Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x1e30a241296f07f9e4e702f5066031ba128e163fd7858cfd09311ddff14bebf8
Migrations: 0xe1b914764eeed3cceee23a6b8b43365318b219a9
Saving successful migration to network...
... 0x74086dea1bf5aab373d9091512fea7f84188c05f6c2c071eae45a7eb3f5abc5d
Saving artifacts...
Running migration: 1524796297_will_contract.js
Error encountered, bailing. Network state unknown. Review successful transactions manually.
TypeError: contract.detectNetwork is not a function
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/actions/deploy.js:6:1
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/deferredchain.js:20:1
它编译得很好,但是当迁移是 运行 时,它声称找不到 contract.detectNetwork。这很奇怪,因为我可以找到方法。它在 node_modules/truffle-contract/contract.js
detectNetwork: function() {
var self = this;
return new Promise(function(accept, reject) {
// Try to detect the network we have artifacts for.
if (self.network_id) {
// We have a network id and a configuration, let's go with it.
if (self.networks[self.network_id] != null) {
return accept(self.network_id);
}
}
self.web3.version.getNetwork(function(err, result) {
if (err) return reject(err);
var network_id = result.toString();
// If we found the network via a number, let's use that.
if (self.hasNetwork(network_id)) {
self.setNetwork(network_id);
return accept();
}
// Otherwise, go through all the networks that are listed as
// blockchain uris and see if they match.
var uris = Object.keys(self._json.networks).filter(function(network) {
return network.indexOf("blockchain://") == 0;
});
var matches = uris.map(function(uri) {
return BlockchainUtils.matches.bind(BlockchainUtils, uri, self.web3.currentProvider);
});
Utils.parallel(matches, function(err, results) {
if (err) return reject(err);
for (var i = 0; i < results.length; i++) {
if (results[i]) {
self.setNetwork(uris[i]);
return accept();
}
}
// We found nothing. Set the network id to whatever the provider states.
self.setNetwork(network_id);
accept();
});
});
});
}
我的迁移文件在这里:
var Will = artifacts.require("./Will.sol");
module.exports = function(deployer, accounts) {
var password1 = "who";
var password2 = "dat";
var deadline = 10;
deployer.deploy(password1, password2, deadline, {value: 100, from: accounts[0]});
};
Will.sol 文件的构造函数:
//Constructor function that initializes passwords, deadline, and defines destination account
function Will(bytes32 password1, bytes32 password2, uint _deadline) payable {
deadline = _deadline;
pass1 = password1;
pass2 = password2;
owner = msg.sender;
}
truffle.js:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*" // Match any network id
}
}
};
Adam 提供的解决方案 100% 正确。
deployer.deploy(password1, password2, deadline, {value: 100, from: accounts[0]});
应该是
deployer.deploy(will, password1, password2, deadline, {value: 100, from: accounts[0]});
尝试 运行 truffle 迁移时,出现以下错误:
Using network 'development'.
Running migration: 1_initial_migration.js
Replacing Migrations...
... 0x1e30a241296f07f9e4e702f5066031ba128e163fd7858cfd09311ddff14bebf8
Migrations: 0xe1b914764eeed3cceee23a6b8b43365318b219a9
Saving successful migration to network...
... 0x74086dea1bf5aab373d9091512fea7f84188c05f6c2c071eae45a7eb3f5abc5d
Saving artifacts...
Running migration: 1524796297_will_contract.js
Error encountered, bailing. Network state unknown. Review successful transactions manually.
TypeError: contract.detectNetwork is not a function
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/actions/deploy.js:6:1
at /usr/local/lib/node_modules/truffle/build/webpack:/~/truffle-deployer/src/deferredchain.js:20:1
它编译得很好,但是当迁移是 运行 时,它声称找不到 contract.detectNetwork。这很奇怪,因为我可以找到方法。它在 node_modules/truffle-contract/contract.js
detectNetwork: function() {
var self = this;
return new Promise(function(accept, reject) {
// Try to detect the network we have artifacts for.
if (self.network_id) {
// We have a network id and a configuration, let's go with it.
if (self.networks[self.network_id] != null) {
return accept(self.network_id);
}
}
self.web3.version.getNetwork(function(err, result) {
if (err) return reject(err);
var network_id = result.toString();
// If we found the network via a number, let's use that.
if (self.hasNetwork(network_id)) {
self.setNetwork(network_id);
return accept();
}
// Otherwise, go through all the networks that are listed as
// blockchain uris and see if they match.
var uris = Object.keys(self._json.networks).filter(function(network) {
return network.indexOf("blockchain://") == 0;
});
var matches = uris.map(function(uri) {
return BlockchainUtils.matches.bind(BlockchainUtils, uri, self.web3.currentProvider);
});
Utils.parallel(matches, function(err, results) {
if (err) return reject(err);
for (var i = 0; i < results.length; i++) {
if (results[i]) {
self.setNetwork(uris[i]);
return accept();
}
}
// We found nothing. Set the network id to whatever the provider states.
self.setNetwork(network_id);
accept();
});
});
});
}
我的迁移文件在这里:
var Will = artifacts.require("./Will.sol");
module.exports = function(deployer, accounts) {
var password1 = "who";
var password2 = "dat";
var deadline = 10;
deployer.deploy(password1, password2, deadline, {value: 100, from: accounts[0]});
};
Will.sol 文件的构造函数:
//Constructor function that initializes passwords, deadline, and defines destination account
function Will(bytes32 password1, bytes32 password2, uint _deadline) payable {
deadline = _deadline;
pass1 = password1;
pass2 = password2;
owner = msg.sender;
}
truffle.js:
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*" // Match any network id
}
}
};
Adam 提供的解决方案 100% 正确。
deployer.deploy(password1, password2, deadline, {value: 100, from: accounts[0]});
应该是
deployer.deploy(will, password1, password2, deadline, {value: 100, from: accounts[0]});